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);
        }
     
}

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.

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.

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

fstream file( fileName.c_str() );
     string text;
     
     stringstream parse (stringstream::in | stringstream::out);
      
      
     int lineNumber = 0;
     
     while (getline(file, text, '\n') && lineNumber <= 2 )
           {
           
            ++lineNumber;
           
           
           
          while (getline(file, text, ',') && lineNumber >= 3 ) 
           {
           parse << text;
           data.push_back(text);
           data.push_back("  ");
           }

(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.)

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

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.