473,511 Members | 15,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading delimited text files?

59 New Member
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 3579

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

Similar topics

3
7016
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files...
1
6661
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
3
3782
by: Leonard Wright | last post by:
I'm developing a lottery program and want read a text file that has Draw date Draw number six lottery numbers all in comma delimited text file I want the user to select a draw number to show...
1
2384
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
8
3182
by: stephane8 | last post by:
I'm able to open and read my .csv file. But when the character "-" is in one of my fields (ex : 18802-002) I get a system.DBNull value ! Even if I edit the file with notepad and put "18802-002" I...
0
1664
by: Masa Ito | last post by:
I have pipe delimited (and comma/tab) files that I read with JET using a schema file. Occasionally a field has multiple quotes (") inside a single field - which chokes the line (the rest of the...
2
3210
by: Wes Peters | last post by:
Does anyone know of an article that deals with the subject of reading a structured text file using VBA code in Access? Thanks, Wes
2
4547
by: Clive Green | last post by:
Hello peeps, I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and writing MP3 tags). I have been using PHP on the command line (Mac OS X Unix shell, to be precise), and am...
6
22486
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
1
2202
by: yesvee | last post by:
Hello All, I'm new for Python programming and I'm having some issues parsing a delimited text file. Any help, suggestions and advice is really appreciated. Thanks a lot in advance. ...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.