0

My program scans names and birth years and stores them in an array of structures. Scanning from keyboard and printing in screen works fine, but I'm not sure whether printing in my binary file is correct because my program runs without errors and I can't check if the data has been printed correctly in the binary file. My question is about whether the syntax of my "fwrite" functions is correct.

#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);
}

This program reads from the bin file

    #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);           
}
4

2 回答 2

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 等。

于 2013-10-03T06:55:42.193 回答
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;
}
于 2013-10-02T21:40:12.163 回答