471,873 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

reading delimited text files?

I have a file that looks like this:
string string 0 1 34
string string 0 4 5 8 3 4

the 2nd number from the left shows how many numbers are going to follow it.
i.e. on the 2nd line, 4 has 5 8 3 4.

Does anybody know a way to read the file? I'm trying to read it and put the data in a two dimensional array. What i have is the following function:
Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------
  2. // This function takes student data file and reads the
  3. // data in the two dimensional array for further processing
  4. //---------------------------------------------------------
  5. int stdLoadData(ifstream &ifile, string stdArray[][11]) {
  6.     string firstName, lastName, studentID, numOfCourses, course1, course2, course3, course4, course5, course6;
  7.     int j = 0;
  8.     string temp;
  9.  
  10.     while (!ifile.eof()) {
  11.         getline(ifile,firstName,' ');
  12.         stdArray[j][stdFirstName] = firstName;
  13.         getline(ifile,lastName,' ');
  14.         stdArray[j][stdLastName] = lastName;
  15.         getline(ifile,studentID,' ' );
  16.         stdArray[j][stdStudentID] = studentID;
  17.         getline(ifile,numOfCourses, ' ');
  18.         stdArray[j][stdNumOfCourses] = numOfCourses;
  19.  
  20.         if (numOfCourses == "1") { 
  21.                getline(ifile,course1);
  22.                stdArray[j][stdCourse1] = course1;
  23.         } else if (numOfCourses == "2") {
  24.                 getline(ifile,course1, ' ');
  25.                 stdArray[j][stdCourse1] = course1;
  26.                getline(ifile,course2);
  27.                 stdArray[j][stdCourse2] = course2;
  28.         } else if (numOfCourses == "3") {
  29.                 getline(ifile,course1, ' ');
  30.                 stdArray[j][stdCourse1] = course1;
  31.                 getline(ifile,course2, ' ');
  32.                 stdArray[j][stdCourse2] = course2;
  33.                 getline(ifile,course3);
  34.                 stdArray[j][stdCourse3] = course3;
  35.         } else if (numOfCourses == "4") {
  36.                 getline(ifile,course1, ' ');
  37.                 stdArray[j][stdCourse1] = course1;
  38.                 getline(ifile,course2, ' ');
  39.                 stdArray[j][stdCourse2] = course2;
  40.                 getline(ifile,course3, ' ');
  41.                 stdArray[j][stdCourse3] = course3;
  42.                 getline(ifile,course4);
  43.                 stdArray[j][stdCourse4] = course4;
  44.         } else if (numOfCourses == "5") {
  45.                 getline(ifile,course1, ' ');
  46.                 stdArray[j][stdCourse1] = course1;
  47.                 getline(ifile,course2, ' ');
  48.                 stdArray[j][stdCourse2] = course2;
  49.                 getline(ifile,course3, ' ');
  50.                 stdArray[j][stdCourse3] = course3;
  51.                 getline(ifile,course4, ' ');
  52.                 stdArray[j][stdCourse4] = course4;
  53.                 getline(ifile,course5);
  54.                 stdArray[j][stdCourse5] = course5;
  55.         } else if (numOfCourses == "6") {
  56.                 getline(ifile,course1, ' ');
  57.                 stdArray[j][stdCourse1] = course1;
  58.                 getline(ifile,course2, ' ');
  59.                 stdArray[j][stdCourse2] = course2;
  60.                 getline(ifile,course3, ' ');
  61.                 stdArray[j][stdCourse3] = course3;
  62.                 getline(ifile,course4, ' ');
  63.                 stdArray[j][stdCourse4] = course4;
  64.                 getline(ifile,course5, ' ');
  65.                 stdArray[j][stdCourse5] = course5;
  66.                 getline(ifile,course6);
  67.                 stdArray[j][stdCourse6] = course6;
  68.         }
  69.     j++;
  70.     }
  71.     return j;
  72. }
  73.  
which works unless the numOfCourses variable which is the 2nd number on every row, is 0. Then, since there is nothing after it, it doesn't take it as the end of line and read the 1st field from the next line.
Is there a way to fix that?
Thank you!
Sep 5 '06 #1
0 3512

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by John B. Lorenz | last post: by
3 posts views Thread by Leonard Wright | last post: by
2 posts views Thread by Wes Peters | last post: by
1 post views Thread by yesvee | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.