C 从文件读取到数组

C 从文件读取到数组,c,io,fread,C,Io,Fread,我正试图从文件中读取。我正在使用fread(),但我不确定我是否正确地执行了此操作。我想创建一个结构数组,并继续从文件中“f-reading”到数组中,如下所示: //Get size of file struct stat st; stat(document, &st); int size = st.st_size; //Create appropriate array size of structs struct Person person[size]; for(j = 0;

我正试图从文件中读取。我正在使用fread(),但我不确定我是否正确地执行了此操作。我想创建一个结构数组,并继续从文件中“f-reading”到数组中,如下所示:

//Get size of file
struct stat st;
stat(document, &st);
int size = st.st_size;

//Create appropriate array size of structs

struct Person person[size];

for(j = 0; j < size; j++) {
    fread(person[j].name, 1, 16, fp); //each name is truncated to 15 bytes on the file
    fread(person[j].text, 1, 24, fp);  //text is truncated to 24 on the file
}

我是否正确使用了fread()?谢谢。

下面给出的代码在for循环中已经足够了

fread(person[j], sizeof(struct Person), 1, fp);

下面给出的代码在for循环中就足够了

fread(person[j], sizeof(struct Person), 1, fp);

您应该检查fread函数调用是否成功地从数据流中读取了预期的字节数:

//Get size of file
struct stat st;
int name_bytes_read, text_bytes_read; // If using C11, use size_t instead
stat(document, &st);
int size = st.st_size;


//Create appropriate array size of structs

struct Person person[size];

for(j = 0; j < size; j++) {
    name_bytes_read = fread(person[j].name, 1, 16, fp); //each name is truncated to 15 bytes on the file
    if (name_bytes_read != 16) { 
      fputs ("Error reading name record", stderr); 
      exit(-1);
    }
    text_bytes_read = fread(person[j].text, 1, 24, fp);  //text is truncated to 24 on the file
    if (text_bytes_read != 24) {
      fputs ("Error reading text record", stderr);
      exit(-1); }
}
//获取文件大小
结构统计;
int name_bytes_read,text_bytes_read;//如果使用C11,请使用大小\u t
统计(文件和技术);
int size=st.st\u size;
//创建适当大小的结构数组
结构人[尺寸];
对于(j=0;j

进一步阅读:

您应该检查fread函数调用是否成功地从数据流中读取了预期的字节数:

//Get size of file
struct stat st;
int name_bytes_read, text_bytes_read; // If using C11, use size_t instead
stat(document, &st);
int size = st.st_size;


//Create appropriate array size of structs

struct Person person[size];

for(j = 0; j < size; j++) {
    name_bytes_read = fread(person[j].name, 1, 16, fp); //each name is truncated to 15 bytes on the file
    if (name_bytes_read != 16) { 
      fputs ("Error reading name record", stderr); 
      exit(-1);
    }
    text_bytes_read = fread(person[j].text, 1, 24, fp);  //text is truncated to 24 on the file
    if (text_bytes_read != 24) {
      fputs ("Error reading text record", stderr);
      exit(-1); }
}
//获取文件大小
结构统计;
int name_bytes_read,text_bytes_read;//如果使用C11,请使用大小\u t
统计(文件和技术);
int size=st.st\u size;
//创建适当大小的结构数组
结构人[尺寸];
对于(j=0;j
进一步阅读:

通过sizeof(结构人)增加j,以避免fread问题 或者,您可以使用feof检查文件结尾,以避免fread问题
或者,您可以使用feof来检查文件结尾是否应该是
fread(person+j,sizeof(struct person),1,fp)
第一个参数应该是指针,person不是C中的类型,struct person是。应该是
fread(person+j,sizeof(struct person),1,fp)
第一个参数应该是指针,Person不是C中的类型,struct Person是。谢谢你的建议。在尝试您的建议后,我确实收到一个错误“读取姓名记录时出错”。。。我相信这是最后一次没有东西可读了。我说得对吗?如何修复此问题?fread()可能有点。。。易碎的以这种方式解析数据文件时。您可能会考虑使用FGETSH()一次获取一行,然后用ScScript()来解析该行。谢谢您的建议。在尝试您的建议后,我确实收到一个错误“读取姓名记录时出错”。。。我相信这是最后一次没有东西可读了。我说得对吗?如何修复此问题?fread()可能有点。。。易碎的以这种方式解析数据文件时。您可能会考虑使用FGETSH()一次获取一行,然后用SsChanfor()来解析该行。