简体   繁体   中英

How to read text from a .txt file and then store it in a record (data structure)?

I have a .txt file with every line concluding : StudentID , firstName, lastName and score. i want to write a program in C that reads the .txt file and stores all the information of EACH student in a RECORD (data structure). The problem is that i don't know the condition i have to use in order to read seperately every different element (StudentID , firstName etc.) because they are just seperated with a space ' ' and then there is also the problem that i have to change line to store the next students info ... Any help ?

the following proposed code snippets should be enough to guide you in writing your application.

to compile/link the following code, you will need the header files:

 #include <stdio.h>  // fgets(), fopen(), fclose(), perror()
 #include <stdlib.h> // exit(), EXIT_FAILURE, strtof()
 #include <string.h> // strtok(), strcpy(), strtol()

regarding;

 StudentID , firstName, lastName and score.

so there are 4 fields to input for each student.

How long are each of those fields?

Using a reasonable guess on sizes:

 StudentID is unsigned long integer 
 FirstName is char array, max 30 characters
 LastName  is char array, max 30 characters
 Score     is float

So a struct to hold one student would be:

 struct student
 {
    size_t   StudentID;
    char     FirstName[30];
    char     LastName[30];
    float    Score;
 };

Assuming that the input file is lines of text, then to read a line

 // open the file for reading
 if( !(fp = fopen( "studentFile.txt", "r" ) ) )
 {
     perror( "fopen for student input file failed" );
     exit( EXIT_FAILURE );
 }


 struct student *students = NULL;
 size_t studentCount = 0;

 char buffer[128];
 while( fgets( buffer, sizeof( buffer ), fp ) ) 
 { 

then each line must be broken into the related fields and placed into an instance of the struct

     // increase number of students in array by 1
     struct student * temp = realloc( students, (studentCount+1) * sizeof( struct student ) );
     if( !temp )
     {
         perror( "realloc for new student data failed:" )
         free( students );
         exit( EXIT_FAILURE );
     }

     students = temp;

     char *token = strtok( buffer, " ");
     if( token )
     {
         students[ studentCount ]->StudentID = (size_t)strtol( token, 10 );

         if( (token = strtok( NULL, " " ) )
         {
             strncpy( students[ studentCount ]->FirstName, token. sizeof( student.FirstName) )l

             if( (token = strtok( NULL, " " ) )
             {
                 strncpy( students[ studentCount ]->LastName, token, sizeof( student.LastName );

                 if( (token = strtok( NULL, " " ) )
                 {
                     students[ studentCount ]->Score = strtof( token, NULL ); 
                 }
             }
         }
     }
     studentCount++;
 }

Then all the lines of student information in the input file are now instances in an array of struct student

If you need more assistance, then post any requests for clarity, etc below this answer, as a comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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