Question

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!!

Was it helpful?

Solution

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

OTHER TIPS

I (almost) wish *scanf functions were never invented.

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.

(Granted, in C there are none in the standard library).

Take at look at this SO question: Parse CSV file in C

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top