Hello im having a problem with my program. Im trying to read in two files called list.txt and teachers.txt and set them to an array called studentList[] and teacherList[] but i dont know how to do that, any suggestions? I know i have to use a for loop but i dont know how to scan each line of the file and set it equal to the first element of an array. This is the code i have so far:

typedef struct {
   char StLastName[THIRTY];
   char StFirstName[THIRTY];
   int Grade;
   int Classroom;
} student;

typedef struct {
   char TLastName[THIRTY];
   char TFirstName[THIRTY];
   int Classroom;
} teacher;


int main() {
   student studentList[TWO_HUNDRED];
   teacher teacherList[FIFTY];

   FILE *in;
   FILE *in2;
     
   in = fopen("list.txt","r");
   in2 = fopen("teachers.txt","r");
   
   return 0;
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

"fgets" is what you're looking for, at the very least. the easiest example will read the first line of the text file pointed by your FILE * into an arbitrary variable i call tempString:

fgets(tempString, THIRTY + 1, in)

it will read up to "THIRTY + 1" characters, or until it reaches a newline character, whichever comes first. this assumes that the entire line of the text file contains only one name entry, either first or last, but not both.

if you want to instead fill it into an array, you have to use a loop and increment the array index each time:

for (i=0; i<HOW_MANY_LOOPS; i++)
{
    fgets(studentList[i].StLastName, THIRTY + 1, in);
}

again this is making the major assumption that there is only the student's last name on each line.

the reality is that youre going to have to figure out formatting issues in your text files -- how are the names listed? do the last and first names alternate on separate lines? probably not. are teh last and first names of each student on their own line? probably. how are they separated, with just a space? or a comma? or a comma and a space?

when you have multiple elements on each line youll have to fgets the line first, then parse (separate) each element based on either fixed widths or text delimiters (like commas or whitespace). the functions "strstr" and "strtok" will be useful to parse elements based on delimiters.

the more you get into it the more you'll see that potential variations can really cause you headaches. you'll be tempted at first to assume (or demand) that the text file is strictly formatted according to your hard and fast rules. the real world however will not be so accommodating. The files will likely have variations and your parser will have to be able to interpret variations and perform error checking, so that an unexpected space or tab won't blow your database arrays apart

this should give you a start anyhow.


.

Edited 7 Years Ago by jephthah: n/a

"fgets" is what you're looking for, at the very least. the easiest example will read the first line of the text file pointed by your FILE * into an arbitrary variable i call tempString:

fgets(tempString, THIRTY + 1, in)

it will read up to "THIRTY + 1" characters, or until it reaches a newline character, whichever comes first. this assumes that the entire line of the text file contains only one name entry, either first or last, but not both.

if you want to instead fill it into an array, you have to use a loop and increment the array index each time:

for (i=0; i<HOW_MANY_LOOPS; i++)
{
    fgets(studentList[i].StLastName, THIRTY + 1, in);
}

again this is making the major assumption that there is only the student's last name on each line.

the reality is that youre going to have to figure out formatting issues in your text files -- how are the names listed? do the last and first names alternate on separate lines? probably not. are teh last and first names of each student on their own line? probably. how are they separated, with just a space? or a comma? or a comma and a space?

when you have multiple elements on each line youll have to fgets the line first, then parse (separate) each element based on either fixed widths or text delimiters (like commas or whitespace). the functions "strstr" and "strtok" will be useful to parse elements based on delimiters.

the more you get into it the more you'll see that potential variations can really cause you headaches. you'll be tempted at first to assume (or demand) that the text file is strictly formatted according to your hard and fast rules. the real world however will not be so accommodating. The files will likely have variations and your parser will have to be able to interpret variations and perform error checking, so that an unexpected space or tab won't blow your database arrays apart

this should give you a start anyhow.


.

thank you very much. this really helped me out. I appreciate your help.

This article has been dead for over six months. Start a new discussion instead.