写在二进制文件中

时间:2013-10-02 21:03:41

标签: c binaryfiles

我的程序会扫描姓名和出生年份,并将它们存储在一系列结构中。从键盘扫描和在屏幕上打印工作正常,但我不确定我的二进制文件中的打印是否正确,因为我的程序运行没有错误,我无法检查数据是否已在二进制文件中正确打印。 我的问题是我的“fwrite”函数的语法是否正确。

#include <stdio.h>

#define MAXNAME 50 //size of name
#define MAXPERSONS 2 //Max num of persons

typedef struct{
    char name[MAXNAME];
    int year;
}person_t;

int read_person(person_t[], int);//scans the person
int write_person(const person_t[], int, FILE*);//prints the persons in the screen and the bfile

int main()
{
    FILE *pfile;
    person_t v[3];
    int iscan=0,iprint;

    if((pfile=fopen("persons.bin","wb"))==NULL) printf("couldnt open<vehicles.txt>\n");

    else{
        while(iscan<MAXPERSONS){
            read_person(&v[iscan],iscan+1);
            iscan++;
        }
        for(iprint=0;iprint<iscan;iprint++)
            write_person(&v[iprint],iprint+1,pfile);
    }
    fclose(pfile);
    printf("\n\n");
    return 0;
}

int read_person(person_t v[],int i)
{
    printf("Person %d",i);
    printf("\n\tName: ");
    fflush(stdin);
    gets(v->name);
    printf("\n\tYear: ");
    scanf("%d",&v->year);
}

int write_person(const person_t v[],int j, FILE *pfile)
{
    //print in screen
    printf("\nPerson %d",j);
    printf("\n\tName: %s\n",v->name);
    printf("\n\tYear: %d\n",v->year);

    //print in the binary file
    fwrite(v->name,sizeof(char),1,pfile);
    fwrite(&v->year,sizeof(int),1,pfile);
}

此程序从bin文件中读取

    #include<stdio.h>

#define MAXNAME 50 //size of name
#define MAXPERSONS 2 //Max num of persons

typedef struct{
    char name[MAXNAME];
    int year;
}person_t;

int read_person(person_t[], int, FILE*);
int write_person(const person_t[], int);
int main(){
    FILE *pfile;
    person_t v[3];
    int iscan=0,iprint;

    if((pfile=fopen("persons.bin","rb"))==NULL) printf("couldnt open<vehicles.txt>\n");

    else{
        while(iscan<MAXPERSONS){
        read_person(&v[iscan],iscan+1,pfile);
        iscan++;
        }
        for(iprint=0;iprint<iscan;iprint++)
            write_person(&v[iprint],iprint+1);
    }
fclose(pfile);
printf("\n\n"); 
return 0;   
}
int read_person(person_t v[],int i, FILE *pfile){
    //read from the binary file
    fread(v->name, sizeof(v->name),1,pfile);
    fread(&v->year,sizeof(v->year),1,pfile);
}
int write_person(const person_t v[],int j){
    //print in screen
     printf("\nPerson %d",j); 
        printf("\n\tName: %s\n",v->name);  
        printf("\n\tYear: %d\n",v->year);           
}

2 个答案:

答案 0 :(得分:0)

建议更改fwite()以写出person_t的完整大小。

int write_person(const person_t v[], int j, FILE *pfile) {
  //print in screen
  printf("\nPerson %d",j);
  printf("\n\tName: %s\n",v[j].name);
  printf("\n\tYear: %d\n",v[j].year);

  //print in the binary file
  if (1 != fwrite(&v[j], sizeof(v[j]),1, pfile)) handle_error();
  return 0;
}

int read_person(person_t v[], int i) {
  printf("Person %d",i);
  printf("\n\tName: ");
  //  don't do this fflush(stdin);
  // consider scanf() gets(v.name);
  // " %49[^\n]": space to consume leading whitespace, 49 (50 -1 ) limit input, [^\n] to read any text but \n
  if (1 != scanf(" %49[\n]", v[i].name)) handle_error();
  printf("\n\tYear: ");
  if (1 != scanf("%d",&v[i].year)) handle_error();
  return 0;
}

答案 1 :(得分:0)

您可以考虑编写和阅读整个person_t结构。

int write_person(const person_t v[],int j, FILE *pfile)
{
    //print in screen
    printf("\nPerson %d",j);
    printf("\n\tName: %s\n",v[j].name);
    printf("\n\tYear: %d\n",v[j].year);
    fwrite(&(v[j]),sizeof(person_t),1,pfile);
}

您正在接近的一般想法称为“序列化”或“编组”(取决于作者)。有许多方法可以序列化或编组数据,但是一些好的方法是JSON,UBJSON,MessagePack和ProtocolBuffers等。