specify line of file to start reading?

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

Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

specify line of file to start reading?

 
0
  #1
Jun 7th, 2008
Hi, I have a readFile() function that reads a csv file into a vector.

The first two lines of the csv are collum headings.

After reading and displaying the file on screen, replacing ","'s with spaces, it asks if the user wants to append new entries. If yes, it calls appendFile() which collects the entries and then calls writeFile() to append to the file.

The problem is, when it first reads existing file, it puts the headings into the vector to, which means it rewrites them when it appends.

So what I want to do is have my readFile() start reading at the third line since if the file exists, it will have headings in the first two lines. I have another function , createFile() Which makes the original file with headings already in it.

Here is my readFile():

void readFile(string)
{
     
     
     
     cout << "Type the name of the file.\n";
    
     getline(cin, fileName);
     
     
     fstream file( fileName.c_str() );
     string text;
     stringstream parse (stringstream::in | stringstream::out);
     while (getline(file, text, ','))
           {
                            
           parse << text;
           data.push_back(text);
           data.push_back("  ");
                    
           }
     
      for (iter = data.begin(); iter != data.end(); ++iter)
      cout << *iter;
     
     char yesNo;
     cout << "\n\n";
     cout << "Do you want to add another entry to the file?\n";
   
     cin >> yesNo;
   
     
     if (yesNo == 'y' || yesNo == 'Y')
        {
               appendToFile(fileName, data);
        }
     
}
Last edited by iansane; Jun 7th, 2008 at 1:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: specify line of file to start reading?

 
0
  #2
Jun 7th, 2008
Why not simplify, and just write the whole file?
Unless you're incrementally adding (always) to a file which is many megabytes in size, this isn't going to take much longer than any other approach.

Or how about a boolean, which records whether a header exists in the file (or not).

Or for each entry in your vector, a boolean as to whether that line exists in the file already (or not). If not, then append it to the file and set the flag.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: specify line of file to start reading?

 
0
  #3
Jun 7th, 2008
sorry, I don't know anything about flags or how to use a boolean to check if the line already exists.

I'm not sure how to check for headers. There's nothing special about them. I just added lines when first creating the file so don't know how program can tell the difference.

I'm appending data to the end of the file.
If I write the whole file which is what it does now, the headers get appended too.

I want it simple so I thought if there was a way to start reading at line number 3 that would be pretty simple. Something like "when (line number == 3) {//read into vector}"

I know there is not a "when" statement but that is what I want it to do. Start reading into the vector at line 3.

Is there a way to put that into c++ syntax?

I'm a newb and this took me days to get working. I don't know how else to rewrite the code at this point.
Last edited by iansane; Jun 7th, 2008 at 3:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: specify line of file to start reading?

 
0
  #4
Jun 7th, 2008
I tried this and it is counting and skipping the first two lines but now the second "while" is not using commas as a delimeter. It's reading them into the vector. How can I delimit with '\n' and then with commas? Thanks

  1. fstream file( fileName.c_str() );
  2. string text;
  3.  
  4. stringstream parse (stringstream::in | stringstream::out);
  5.  
  6.  
  7. int lineNumber = 0;
  8.  
  9. while (getline(file, text, '\n') && lineNumber <= 2 )
  10. {
  11.  
  12. ++lineNumber;
  13.  
  14.  
  15.  
  16. while (getline(file, text, ',') && lineNumber >= 3 )
  17. {
  18. parse << text;
  19. data.push_back(text);
  20. data.push_back(" ");
  21. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: specify line of file to start reading?

 
0
  #5
Jun 7th, 2008
(Sorry, I've got a splitting headache so I haven't really looked at your code...)

For a CSV file you know that the headers are there. If you load all lines into the vector then it is easy to delete them before saving again. (While a vector works fine for this, a better choice would be to use a deque. It looks exactly like a vector but it is better for handling insertions and deletions at both front and back of the list.)

  1.  
  2. // Load my list of records from the CSV file
  3. deque <record_t> csv_data = read_csv_file( ... );
  4. ...
  5. // Get rid of those two header records at the front of the array...
  6. csv_data.erase( csv_data.begin(), csv_data.begin() +2 );
  7. // ...and write what is left back to file.
  8. write_csv_file( csv_data, ... );

Hope this helps.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC