简体   繁体   English

C从文本中读取多个结构

[英]C Reading multiple structs from text

Hey I have been trying to read multiple structs from a text file and count how many structs have been read in my project. 嘿,我一直在尝试从一个文本文件中读取多个结构,并计算在我的项目中已经读取了多少个结构。

this is my struct: 这是我的结构:

#define Input_Length_fileName 25 /*maximum letter for Input*/ 
#define Input_Length_description 80 /*maximum letter for Input*/ 
#define DataSize 200 /*maximum letter for one pic*/ 
#define Maximun_Picture 20 /*maximum photos in recoude*/

typedef struct picture_Data
{
char fileName[Input_Length_fileName];
char description[Input_Length_description];
char location[Input_Length_fileName];
int peopleCount;
}pic;

this is the reading structs function: 这是阅读结构功能:

#define PIC_FILE "Pic.dat"
int readFile (pic *entries, int pic_order){

FILE * infile;
int count = 0;
char line[DataSize];

infile = fopen(PIC_FILE, "rb");

if (infile != NULL)
{
    count = fread(entries, sizeof(pic), Maximun_Picture, infile);

    while (fgets(line, DataSize, infile)!=NULL)
    {

        sscanf(line, "%s,%s,%s,%d", &entries[pic_order].fileName, &entries[pic_order].description, &entries[pic_order].location, &entries[pic_order].peopleCount);              
    }

    fclose(infile);
}
entries[count].fileName[0]= '\0';

return count;
}

this is how the text file 这是文本文件的方式

 photo1,I love this photo1,Helsinki,20
 photo2,I love this photo2,Pari,30

this is how I call it 这就是我所说的

for ( i = 0; i < Maximun_Picture; i++)
  {
     pic_recorded_number = readFile(&picture_record[i],i);
  }


    printf("%d\n", pic_recorded_number);
    printf("%d\n", picture_record[0].peopleCount);

I know it is not right. 我知道这是不对的。 Can somebody tell me what s wrong and how to fix it? 有人可以告诉我哪里出了问题以及如何解决吗? Thank you very mych!! 非常感谢你!

I (almost) wish *scanf functions were never invented. 我(几乎)希望* scanf函数从未被发明。

This will easily break; 这很容易打破; consider a , in the description (or any of the string fields). 考虑,在本说明书中(或任何的字符串字段的)。

Make life easy on yourself and don't reinvent the wheel: use a library that implements a common data interface, such as CSV. 让自己的生活变得轻松,并且不要浪费时间:使用实现通用数据接口(例如CSV)的库。

(Granted, in C there are none in the standard library). (当然,在C中标准库中没有任何内容)。

Take at look at this SO question: Parse CSV file in C 看看这个SO问题: 用C解析CSV文件

#include <stdio.h>

#define Input_Length_fileName 25 /*maximum letter for Input*/ 
#define Input_Length_description 80 /*maximum letter for Input*/ 
#define DataSize 200 /*maximum letter for one pic*/ 
#define Maximun_Picture 20 /*maximum photos in recoude*/

typedef struct picture_Data{
    char fileName[Input_Length_fileName];
    char description[Input_Length_description];
    char location[Input_Length_fileName];
    int peopleCount;
} pic;

#define PIC_FILE "Pic.dat"

int readFile (pic *entries){//[, int pic_order] : Not required If you are reading all.
    FILE * infile;
    int count = 0;
    char line[DataSize];

    if(NULL == (infile = fopen(PIC_FILE, "r")))// "r" : text file
        return 0;
    //count = fread(entries, sizeof(pic), Maximun_Picture, infile);//for fwrite

    while (fgets(line, DataSize, infile)!=NULL){
        sscanf(line, " %[^,],%[^,],%[^,],%d",
        entries[count].fileName, entries[count].description, entries[count].location, &entries[count].peopleCount);
        if(++count >= Maximun_Picture)
            break;
    }
    fclose(infile);
    return count;
}

int main(){
    pic picture_record[Maximun_Picture];
    int pic_recorded_number = readFile(picture_record);

    printf("%d\n", pic_recorded_number);
    printf("%s\n", picture_record[0].description);
    printf("%d\n", picture_record[0].peopleCount);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM