如何存储或打印二进制* .dat文件中的信息? C编程

时间:2014-03-26 04:40:41

标签: c data-structures stream fopen binaryfiles

我想存储或读取处于二进制模式的* .dat文件中的信息。该文件具有以下数据(以二进制形式编码):

License: 123456
Owner: John Doe
Value: 10000.00

License: IAMDOE
Owner: Jane Doe
Value: 20000.00

这是我的代码:

#include <stdio.h>
#include <string.h>
#define MAXPLEN 80

typedef struct {    
    char none; /* used for registry initiated by '\0' ,should be ingnored */    
    char owner[MAXPLEN];
    char license[6];
    double value;
} veiculo_t;    


/* i need to use the following functions in the process */

void print_registry(veiculo_t *v)
{
    printf("Owner: %s - License: %.6s - Value: %.2lf \n", (*v).owner, (*v).license, (*v).value);
}

void read_registry(veiculo_t *v)
{
    char license2[8];
    printf("Name of the owner:\n");
    fgets((*v).owner,100,stdin);

    printf("License plate:\n");
    fgets(license2,10,stdin);
    memcpy(&(v->license),license2,6);

    printf("Value:\n");
    scanf("%lf",&(*v).value);
}

int op_menu() 
{
    int op;
    printf("\n0 - end\n");
    printf("1 - insert\n");
    printf("2 - print\n");
    printf("option: ");
    scanf("%d",&op);  
    getchar();        
    return op;
}

int main()
{
    int op;
    op_menu();

    if (op=1){
        /* !!!!!HELP HERE!!!!!! */
    }

    if (op=2){
        FILE *f=fopen("veic.dat", "rb");
        if (f == NULL) 
        {
            printf("Not opened!\n");
        }
        else
            /* !!!!!HELP HERE!!!!!! */
    }
}

不太了解&#34;溪流&#34; ...一些提示会被贬低!

2 个答案:

答案 0 :(得分:0)

fwritefread可用于满足您的需求。

这是一个示例程序,说明了它们的用法:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
   FILE* out = fopen("data.o", "wb");
   int i = atoi(argv[1]);
   fwrite(&i, sizeof(int), 1, out);
   fclose(out);

   FILE* in = fopen("data.o", "rb");
   fread(&i, sizeof(int), 1, in);
   fclose(in);

   printf("Number saved and restored from file: %d\n", i);
}

执行程序的结果:

>> ./test-51 2345
Number saved and restored from file: 2345
>> ./test-51 -200
Number saved and restored from file: -200

正如您所料,输出文件的大小只有4个字节。

>> wc data.o
0 1 4 data.o

答案 1 :(得分:0)

首先,更改以下内容,否则您将永远不会进入“菜单”。

op = op_menu();    //op should be assigned by the return value of open_menu
if (op==1){...
if (op==2){...    // I believe you want '==' not '='

根据评论,您必须调用read_registry方法。添加的代码:

    if (op==1){
        //insert
        veiculo_t *t = malloc(sizeof(veiculo_t));
        read_registry(t);
        FILE *f=fopen("veic.dat", "ab");
        if (f == NULL)
        {
            printf("open file failed\n");
        }
        fwrite(t, sizeof(char), sizeof(*t), f);
        free (t);
    }

    if (op==2){
        //print
        FILE *f=fopen("veic.dat", "rb");
        if (f == NULL)
        {
            printf("Not opened!\n");
        }
        else
        {
            veiculo_t *t = malloc(sizeof(veiculo_t));
            printf("sizeof t: %d\n", sizeof(*t));
            while(fread(t, sizeof(char), sizeof(*t), f))
                print_registry(t);
            free(t);
        }

示例代码仅供您参考,我想提供一个思考而不是答案,但似乎代码总是清晰而有力。在下面的示例中,我编写文件使用附加模式,如果要每次重写文件,请在写入文件时在fopen中更改模式。

希望有帮助。