| | |
specify line of file to start reading?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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():
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.
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.
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.
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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'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.
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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
C++ Syntax (Toggle Plain Text)
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.)
Hope this helps.
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.)
C++ Syntax (Toggle Plain Text)
// 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.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Read part of text file (PHP)
- how do I have txt file be read by the compiler? (C++)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- Reading in certain columns from CSV files into array C++ (C++)
- CSV file (C)
- Reading an input file as a class memeber function (C++)
- Error reading in file in binary mode (C++)
Other Threads in the C++ Forum
- Previous Thread: dynamic buttons
- Next Thread: Writing a string to binary
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






