Search code examples
cmemory-managementstructc99ansi-c

How to read from text file and load it into memory using a struct?


I have a text file like these:

Naruto, Wind, 85, 48, 35, 63
Neji, Fire, 57, 53, 81, 45
Gaara, Earth, 73, 47, 96, 50
Sasuke, Fire, 68, 82, 72, 41
Kankurou, Water, 59, 42, 97, 60

and i have my struct

typedef struct ninja{ 
    char* name; 
    char* element; 
    int ninjutsu ; 
    int genjutsu ; 
    int taijutsu ; 
    int defesa ; 
} Ninja ;

I need to read the text file and load it directly into memory using this struct. Should I create some arrays, store the info inside char by char, and then convert the types (when int) and then load it?.

void file(){
    FILE *file = fopen("ninjas.txt", "r");
    int c;
    if (file) {

       while ((c = getc(file)) != ','){
          putchar(c);
       }
       while ((c = getc(file)) != ','){
          putchar(c);
       }
       while ((c = getc(file)) != ','){
          putchar(c);
       }
       while ((c = getc(file)) != ','){
          putchar(c);
       }
       while ((c = getc(file)) != ','){
          putchar(c);
       }
       while ((c = getc(file)) != '\n'){
          putchar(c);
       }

   fclose(file);
}

}


Solution

  • Your problem may be in parsing of input after reading from file. I write a solution which run perfectly in my system (Ubantu using gcc). I used fgets() method for read a line then parse it using strtok() function.

    Here is code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_RECORD 100
    
        #define MAX_LINE 200
        typedef struct ninja{ 
            char* name; 
            char* element; 
            int ninjutsu ; 
            int genjutsu ; 
            int taijutsu ; 
            int defesa ; 
        } Ninja ;
    
            int main(int argc,char** argv){
    
                    FILE *fptr=fopen("input.txt","r");
                    if(fptr==NULL){
                    printf("Error in openning file\n");
                    return 1;
                    }
                    Ninja ninjas[MAX_RECORD];
                    int total_record=0;
                    char line_buffer[MAX_LINE];
                    while(total_record< MAX_RECORD && fgets(line_buffer,MAX_LINE,fptr)!=NULL)
                    {
                            char *curr_record=NULL;
                            curr_record=strtok(line_buffer,",");
                            ninjas[total_record].name=(char *)malloc(sizeof(char)*50);
                            strcpy(ninjas[total_record].name,curr_record);
                            curr_record=strtok(NULL,",");
                            ninjas[total_record].element=(char *)malloc(sizeof(char)*50);
                            strcpy(ninjas[total_record].element,curr_record);
                            curr_record=strtok(NULL,",");
                            ninjas[total_record].ninjutsu=atoi(curr_record);
                            curr_record=strtok(NULL,",");
                            ninjas[total_record].genjutsu=atoi(curr_record);
                            curr_record=strtok(NULL,",");
                            ninjas[total_record].taijutsu=atoi(curr_record);
                            curr_record=strtok(NULL,",");
                            ninjas[total_record].defesa=atoi(curr_record);
                            total_record++;
                    }
                    for(int i=0;i<total_record;i++)
                    {
                            printf("%s %s %d %d %d %d",ninjas[i].name,ninjas[i].element,ninjas[i].ninjutsu,ninjas[i].genjutsu,ninjas[i].taijutsu,ni$
                            printf("\n");
                    }
                    fclose(fptr);
                    return 0;
            }
    

    Output:

    Naruto  Wind 85 48 35 63
    Neji  Fire 57 53 81 45
    Gaara  Earth 73 47 96 50
    Sasuke  Fire 68 82 72 41
    Kankurou  Water 59 42 97 60