C数据库。没有完全填满二进制文件

我有一个data.txt文件,我用制表符分隔每个字段。我从文件中读取变量,然后写入二进制文件。问题是二进制文件没有完全填充。

person_dump.c:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "person.h"

int readperson(person *p){
        return scanf("%[^\t]\t%d\t%[^\t]\t%d\t%[^\t]\t%d\t%[^\t]\t%d\t%d\t%d\t%[^\t]\n",
        p->Name,&p->NumCPU,p->TypeCPU,&p->SizeRAM,p->TypeGPU,&p->SizeGPU,p->TypeHDD,&p->NumHDD,&p->SizeHDD,&p-> Peripheral,p->NameOS);
}

int main(int argc, char* argv[]){
        person p;
        FILE *out = fopen(argv[1],"w");
        if(!out){
                perror("Can't open file");
                return 2;
        }
        while(readperson(&p)){
                fwrite(&p,sizeof(p), 1,out);
                return 0;
        }
}

人.h:

#ifndef __person_h__
#define __person_h__
typedef struct{
        char Name[50];
        int NumCPU;
        char TypeCPU[50];
        int SizeRAM;
        char TypeGPU[50];
        int SizeGPU;
        char TypeHDD[50];
        int NumHDD;
        int SizeHDD;
        int Peripheral;
        char NameOS[50];
} person;
#endif

终奌站:

rusya@DESKTOP:~/stud/sem2/kp6$ gcc persons_dump.c
rusya@DESKTOP:~/stud/sem2/kp6$ ./a.out data.bin < Data.txt
rusya@DESKTOP:~/stud/sem2/kp6$ cat data.bin
Aminov  0          ﱠ0          ` a         Intel Core i5   6       external       `  `   SATA    1       500     2       Linux
Arapov  2        SATA    1       1000    4       Windows/Linux
Bakshe   8 a                 0      0              0  0
rusya@DESKTOP:~/stud/sem2/kp6$ cat Data.txt
Aminov  4       Intel Core i5   6       external        2       SATA    1       500     2       Linux
Arapov  2       Intel Core i3   4       interior        2       SATA    1       1000    4       Windows/Linux
Baksheev        4       Intel Core i5   8       external        4       SATA    1       1000    2       Windows/Linux
Belov   6       Intel Core i7   8       external        4       SATA    1       1000    4       Windows/Linux
Velesov 4       Intel Core i7   6       external        6       SATA    1       2000    2       Windows/Linux
Gaptulkhakov    6       Intel Core i7   8       interior        4       SATA    1       1000    2       Windows/Linux
评论