c FIle I / O写入函数有另一个字节吗?

时间:2018-04-22 06:49:06

标签: c posix

当我在C中制作学生信息文件时,会出现一些问题。

一个学生信息为char [20],float,int,int(32bytes) 我有1000个学生信息。

如果使用fwrite(FILE * fp)输出文件是正确的32,000字节但是 使用write(int fd)输出文件是32,021字节。什么是21个字节??(我使用Visual Studio 2017)

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>

typedef struct{
    unsigned int ID;
    char name[20];
    float score;
    int age;
} Student;

Student student;

int main(int args,char* argv[]){
 FILE* smp;
 fp = fopen("Sample.txt","r")    // sample.txt is consist of number of information, name,ID,score,age line by line

 int fd = open("out_student.dat",O_WRONLY|O_CREAT);
 //FILE *fp = open("out_student.dat","wb");
 int count = 0;
 fscanf(smp,"%d",&count);
 fseek(smp,2,SEEK_CUR);
 for(int i = 0;i<count;i++)
   {
     memset(student.name, 0, 20);
      for (index = 0; index<128; index++)
      {
        char temp;
        fread(&temp, 1, 1, smp);
        if (temp == ',')
            break;
      }
      fseek(smp, -index - 1, SEEK_CUR);
      fread(&student.name, 1, index, smp);
      fseek(smp, 1, SEEK_CUR);
      fscanf(smp, "%d", &student.ID);
      fseek(smp, 1, SEEK_CUR);
      fscanf(smp, "%f", &student.score);
      fseek(smp, 1, SEEK_CUR);
      fscanf(smp, "%d", &student.age);
      fseek(smp, 2, SEEK_CUR);
      write(fd,&student,sizeof(Student));
      //fwrite(&student,sizeof(Student),1,fp)  this output file is 32,000 bytes
  }    // output file is 32,021!! what is the 21 bytes?

fclose(smp);
close(fd);  //fclose(fp);
return 0;
}

0 个答案:

没有答案