user3404487
user3404487

Reputation: 13

Trouble reading file into array of structs in C

I have an input text file that contains:

1234,123456781,"Smith","Bob A.",ABC, 123,="A01"
1234,123456782,"Jones","Tom Jr",ABC, 123,="A03"  
1234,123456783,"Michaels","Mike",ABC, 123,="A03" 

this is what the output is supposed to look like.

Last Name, First Name            Term      ID      Course   Section 
-------------------------------- ----  ---------   ------   ------- 
Jones, Tom Jr.                   1234  123456782   ABC123     A03 
Michaels, Mike                   1234  123456783   ABC123     A03 
Smile, Bob A.                    1234  123456781   ABC123     A01  

I'm having a lot of trouble reading the information from the text file into an array of structs. It must be implemented in this fashion and then printed/displayed to the user.

Here is my code so far... been working on this for days!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//The values included in the source text file.
typedef struct items{
    char fname[16];
    int cat;
    char lname[16];
    char sub[4];
    int term;
    int  sID;
    char sec[4];
}STRING;

void formatOut(){
    //Output format
    printf("\n\n\nLast Name, First Name            Term      ID      Course   Section");
    printf("\n-------------------------------- ----  ---------   ------   ------- ");
}



int main(){

int count;
count=0;
int i=0;

FILE* fp;
struct items item[200];

//Finds The File
char file_name[25];
printf("Enter the name of file that includes your data. Be sure to write your file extension.\n(For example, source.txt is a valid entry, source is invalid)\n");

gets(file_name);

fp = fopen(file_name,"r");

//Actions to take incase the file does not exist.
if(fp == NULL){
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
//Making things neater.
printf("__________________________________\n\n");
printf("The contents of %s file are :\n", file_name);


//Retrieve data from txt file

do{ fscanf(fp,"%d %d %s %s %s %d %s", &item[i].term, &item[i].sID, item[i].lname, item[i].fname,item[i].sub, &item[i].cat, &item[i].sec);
    printf("Term:%d\n", item[count].term);
    printf("Student ID:%d\n", item[count].sID);
    printf("LastName:%c\n,", item[count].lname);
    printf("FirstName:%s\n", item[count].fname);
    printf("Subject:%s\n", item[count].sub);
    printf("Category:%d\n", item[count].cat);
    printf("Section:%s\n", item[count].sec);
    i++;
}while(fscanf(fp,"%d %d %s %s %s %d %s", &item[i].term, &item[i].sID, item[i].lname, item[i].fname, item[i].sub, &item[i].cat, item[i].sec>1));

/*
for(i=0; i<n; i++){
    fscanf(fp,"%d", &item[count].term);
    fscanf(fp,"%d", &item[count].sID);
    fscanf(fp,"%s", &item[count].lname);
    fscanf(fp,"%s", &item[count].fname);
    fscanf(fp,"%s", &item[count].sub);
    fscanf(fp,"%d", &item[count].cat);
    fscanf(fp,"%s", &item[count].sec);

    printf("Term:%d\n", item[count].term);
    printf("Student ID:%d\n", item[count].sID);
    printf("LastName:%s\n,", item[count].lname);
    printf("FirstName:%s\n", item[count].fname);
    printf("Subject:%s\n", item[count].sub);
    printf("Category:%d\n", item[count].cat);
    printf("Secton:%s\n", item[count].sec);
    count++;
}
 */

fclose(fp);
formatOut();

}

This is what my output currently looks like:

Term:1301
Student ID:0
LastName:\364
FirstName:
Subject:
Category:0
Section:



Last Name, First Name            Term      ID      Course   Section
-------------------------------- ----  ---------   ------   ------- 

Clearly the array of structs is not receiving the correct values. Any help would be much appreciated!

Upvotes: 1

Views: 188

Answers (2)

feihu
feihu

Reputation: 1953

I think the best practice is to set some breakpoints and DEBUG it step by step to see whether there is any problem. This is also the best way to improve your coding and debugging skill.

Please:

  1. change the while statement to:

    while(fscanf(fp,"%d,%d,\"%[^\"]\",\"%[^\"]\",%[^,],%d,=\"%[^\"]\"", &item[i].term, &item[i].sID, item[i].lname, item[i].fname,item[i].sub, &item[i].cat, &item[i].sec)>1)
    {
        printf("Term:%d\n", item[i].term);
        printf("Student ID:%d\n", item[i].sID);
        printf("LastName:%s\n,", item[i].lname);
        printf("FirstName:%s\n", item[i].fname);
        printf("Subject:%s\n", item[i].sub);
        printf("Category:%d\n", item[i].cat);
        printf("Section:%s\n", item[i].sec);
        i++;
    }
    

    the most important change is the format of fscanf. %[\"] means reading the following string until the next ".

    1234,123456789, "  Smith  " , "  Bob A. " , ABC  , 123,= "  A01     "
     |  |    |    | |    |    | | |    |    | |  |   |  | || |   |      |
    %d  ,   %d    , \" %[^\"] \", \" %[^\"] \",%[^,] , %d ,= \" %[^\"]  \"
    
  2. initialize the item: struct items item[200]={0};.

Actually you could try to clean all the warnings, the compiler would give very useful information. Don't ignore them is a very good habit.

Upvotes: 1

Ishmeet
Ishmeet

Reputation: 1610

You should change the lname as an array, same like your fname, these structures have to be filled accordingly to the char array converted to its specific data types.

typedef struct items{
    char fname[16];
    int cat;
    char lname[16];
    char sub[4];
    int term;
    int  sID;
    char sec[4];
}STRING;

you need to convert sID, term, cat using atoi(), since you have an integer in your code and what you will read is an char * from file.

Upvotes: 1

Related Questions