How do I read data from a file if my file is like this with comma separated values
1, 2, 3, 4, 5
6, 7, 8, 9, 10
and after reading the file, I want to write the data back into other file as same format above.
I can get total number of lines, using
string line;
while(!file.eof()){
getline(file,line);
numlines++;
}
numline--; // remove the last empty line
but how can I know total number of digits in a row/line ??
I also have vector of ints to store the data.
So, I want to read the first line and then count total number of elements in that line, here 5 (1,2,3,4,5) and store them in array/vector, and read next line and store them in vector again and so on till I reach EOF.
Then, I want to write the data to file, again, I guess this will do the job of writing data to file,
numOfCols=1;
for(int i = 0; i < vector.size(); i++)
{
file << vector.at(i);
if((numOfCols<5) file << ",";//print comma (,)
if((i+1)%5==0)
{
file << endl;//print newline after 5th value
numOfCols=1;//start from column 1 again, for the next line
}
numOfCols++;
}
file << endl;// last new line
So, my main problem is how to read the data from file with comma separated values ??
Thanks
See Question&Answers more detail:
os