473,666 Members | 2,575 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 3593

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

Similar topics

3
7029
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 into Access. I want to avoid creating a separate "Spec" for each file (there are over 180 files) and instead want to code my own dynamic importing rules. So far it's been going fine, except for one item...
1
6685
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 processing. I am having no luck at all finding different routines written in C to read delimited files of any kind. I have a few ideas of how I might go about this but I bet I'm re-inventing the wheel and there already exists some efficient code out...
3
3792
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 all the stats from that draw number From http://www.developmentnow.com/g/36_2003_7_0_14_0/dotnet-languages-csharp.htm
1
2399
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 server.
8
3192
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 still get a DBNull value... I'm not able to figure this one out, any help would be appreciated. Here's my code : Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath & ";Extended...
0
1672
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 columns get read as nulls). An example of an offending line is: J" Invalid " .... This is a text field that is between the two 'pipes' so I need it read exactly as is. I have reverted to schema files, trying everything I can to overcome this...
2
3218
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
4554
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 getting on more or less OK. So far, I have managed to parse my tab-delimited .txt file properly into an array, and then use this array to update the tags on a bunch of MP3 audio files. Nice.
6
22519
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 Source=C:\dir1A\;Extended Properties=""Text;HDR=No;FMT=Delimited\""" Dim conn1x As New OleDb.OleDbConnection(ConStr) Dim dax1 As New OleDbDataAdapter("Select * from testabc1x.txt", conn1x)
1
2215
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. Objective My input file could be .txt or .dat or .csv Any kind of delimited file with the following delimiters (',','/','|',';',':')
0
8362
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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 we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.