0

I'm working on an app in QT, and I'm kinda new to C++ I/O; I want to read some data from a CSV file that currently has 2 columns and some rows, for example: (1 | 10) (2 | 20) I just want to store them in an array like this (1,10,2,20); for some, only values from the second column get stored in my array. Here is some code; I've checked many times, but I still can't see what I'm doing wrong:

void readingFiles::openFile(){

ofstream myFile;
myFile.open("/home/Dan/Desktop/mainFile.csv" ,std::ios::app);}

void readingFiles::addReadings(int readings1 , int readings2){



    if(myFile.is_open()){

            myFile<<readings1<<","<<readings2<<"\r";
            myFile.close();


}
}

vector<int> values ;
ifstream iFile;
iFile.open("/home/Dan/Desktop/mainFile.csv");}
string data;

void readingFiles::read(){
  for(int i =0; std::getline(iFile,data, ',') ; i++){
             values.insert(values.end() , atoi(data.c_str())); 
             qDebug("vector list is %i" , values[i]);}
}

So, the result I get is that it reads the first row in the first column, then moves to read from the second column, and never goes back to the first (1, 10, 20, 30, 40 etc). Again, what I'm trying to get is (1, 10, 2, 20, 3, 30 etc.).

Dan
  • 11
  • 3
  • 1
    Your code is totally messed up. You're missing a `;` at the end of the `myFile<<` line, and you have unmatched `{` characters. Please post code that will actually compile. – Barmar Jun 01 '16 at 19:01
  • oh im sorry those are typos when i made this question , my main code is fine lol ill fix the post – Dan Jun 01 '16 at 19:02
  • Please also post a readable example of the input file. – Barmar Jun 01 '16 at 19:02
  • 1
    This has absolutely nothing to do with Qt aside from the use of `qDebug`. – MrEricSir Jun 01 '16 at 19:06
  • You still have unmatched braces. There's no closing brace for the `addReadings` and `read` functions. – Barmar Jun 01 '16 at 19:09
  • lol Eric i'm working on qt creator thats why i added the tag , should i remove it ? – Dan Jun 01 '16 at 19:15
  • 1
    Barmar i think fixed the mistakes – Dan Jun 01 '16 at 19:16
  • Possible duplicate of [How to read-write into/from text file with comma separated values](http://stackoverflow.com/questions/1474790/how-to-read-write-into-from-text-file-with-comma-separated-values) – Barmar Jun 01 '16 at 19:19
  • The problem is that you're using comma as the `getline` delimiter. So it skips over the newlines to get to the next comma. See the duplicate question for the proper way to do it. – Barmar Jun 01 '16 at 19:20
  • Why do you use comma as the delimiter when the question says that the numbers are separated by `|`? And what about the parentheses? Is `(1 | 10)` really the format of your file? – Barmar Jun 01 '16 at 19:21
  • 1
    No , excuse my messy question, i typed the (1 | 10) just for the example , i'm not using parentheses or this | in my file , i use excel to read the csv so its just 1 simple integer in each cell, and im using the coma to separate the cells and the \r for new rows – Dan Jun 01 '16 at 19:25
  • i tried the solution in the question you linked and is till have the same problem :( – Dan Jun 01 '16 at 19:59
  • In Qt Creator, please create a new project, with only `main.cpp` in it, and write a minimal, stand-alone, correct test case that reproduces the issue. Make sure you do Ctrl-A, Ctrl-I to indent it properly, then post it here. – Kuba hasn't forgotten Monica Jun 01 '16 at 20:36

1 Answers1

0

Your problem is that on second call getline is returning 10\n2. Then atoi converts this to 10. So you lose the 2. getline is supposed to discard newlines so I'm not sure why this is happening. It's a bit ugly, but parse 10\n2 in data to pick up anything after \n and add to vector.

I tested this with VC 2013 and that is the behaviour of getline with delimiter. I googled around and it does seem as if it is intended behaviour that getline with delimiter treats newline as a literal character. This works:

string line;
for (int i = 0; std::getline(myFile, line); i++)
{
    stringstream ss(line);
    for (int i = 0; std::getline(ss, data, ','); i++)
    {
        values.insert(values.end(), atoi(data.c_str()));
    }
}
for (int i = 0; i < values.size(); i++)
{
    cout << "vec=" << values[i] << endl;
}
John D
  • 1,627
  • 1
  • 11
  • 10