read text file into a structure

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 16
Reputation: dkwantee is an unknown quantity at this point 
Solved Threads: 0
dkwantee dkwantee is offline Offline
Newbie Poster

read text file into a structure

 
0
  #1
Mar 16th, 2008
Hello!
i have the following data in a file

student_id student_name maths_score english_score physics_score

can i use a nested structure to read the file?
  1.  
  2. struct student
  3. { int id;
  4. char name[40];
  5. };
  6. struct score
  7. { int maths;
  8. int english;
  9. int physics;
  10. };
if yes, how will i read the file into the structure?help me please
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: read text file into a structure

 
0
  #2
Mar 16th, 2008
Originally Posted by dkwantee View Post
Hello!
i have the following data in a file

student_id student_name maths_score english_score physics_score

can i use a nested structure to read the file?
  1.  
  2. struct student
  3. { int id;
  4. char name[40];
  5. };
  6. struct score
  7. { int maths;
  8. int english;
  9. int physics;
  10. };
if yes, how will i read the file into the structure?help me please
I'd probably add a data member of type score to the student struct, like this:
  1.  
  2. struct score
  3. { int maths;
  4. int english;
  5. int physics;
  6. };
  7. struct student
  8. { int id;
  9. char name[40];
  10. score studentScore;
  11. };
All five pieces of information could then be stored in a single variable of type student. I guess I'd read from the file as follows, assuming you have an ifstream called ins.
  1. student aStudent;
  2. ins >> aStudent.id;
  3. ins >> aStudent.name;
  4. ins >> aStudent.studentScore.math;
  5. ins >> aStudent.studentScore.english;
  6. ins >> aStudent.studentScore.physics;
Now all of the information is in the variable aStudent.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: dkwantee is an unknown quantity at this point 
Solved Threads: 0
dkwantee dkwantee is offline Offline
Newbie Poster

Re: read text file into a structure

 
0
  #3
Mar 16th, 2008
if i have to sort the scores in maths, do i have to copy it to an array then sort it?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: read text file into a structure

 
0
  #4
Mar 16th, 2008
It might be best to read the file directly into the array of structs. Then when you sort the array by whatever field, all of the data for that student stays together. you can use the qsort() function, or if you want, store the structs in a list, and use the list.sort() function.
Last edited by dougy83; Mar 16th, 2008 at 4:41 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: read text file into a structure

 
0
  #5
Mar 16th, 2008
Originally Posted by dkwantee View Post
if i have to sort the scores in maths, do i have to copy it to an array then sort it?
Well if you have a whole bunch of students and you have an array of type student, I don't see why you couldn't just read it directly into the array.
  1. student someStudents[100];
  2. // miscellaneous code
  3. ins >> someStudents[i].studentScore.math;
Same thing for the other attributes of the student struct. This reads directly into the array, which you can sort by math score later. No need to read it into aStudent and then copy from aStudent to the array. Just read it in directly.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: dkwantee is an unknown quantity at this point 
Solved Threads: 0
dkwantee dkwantee is offline Offline
Newbie Poster

Re: read text file into a structure

 
0
  #6
Mar 16th, 2008
i have tried to read 1 line from a file,and it works. but how to read a whole file if the number of lines in unknown?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC