4

Yesterday I made a small script with some help to read .csv files. I though I found a way to read the first value and store it, but for some reason it stores the last value instead.

I store what I thought should be the first value under value1, and re-display it to make sure it displays properly and is in fact being stored under a callable variable.

Does anyone know what is wrong with this code? I think I should be using vectors but as I read the reference sheets I find on the internet about them I am being a little thrown of. Any help is appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{

    int loop = 1;
    string value;
    string value1;

    while(loop = 1)
    {

        cout << "Welcome! \n" << endl;

        ifstream myfile;

        myfile.open ("C:/Documents and Settings/RHatfield/My Documents/C++/Product Catalog Creator/Source/External/Sample.csv");

        while (myfile.good())

        getline ( myfile, value, ',' );
        cout << string ( value) << endl; 

        value1 = value;

        cout << value1;

        myfile.close();

        system("PAUSE");

        return 0;
    }


}
Jeroen
  • 4,023
  • 2
  • 24
  • 40
Rob
  • 169
  • 4
  • 14

2 Answers2

1

Your error seems to be in pure code formatting.

while (myfile.good())

There is no {} after the loop. So only next line is repeated.

The following code is executed after reading of the whole file.

cout << string ( value) << endl;

Thus value stores the last line of the file.

eugene_che
  • 1,997
  • 12
  • 12
  • Yes your right, but when I fixed it, it does the same thing, only listing all of the values one by one, then storing the last one and re-listing it. Does anyone know where I can find good reference material for this because now I am even more confused by the results. Thanks for the help though, Just a lot of learning ahead of me. – Rob Aug 04 '11 at 15:24
  • @Rob, now it is pure algorithmic problem. There are a lot of solutions. If you want to get the first line only, you do not need any loop. Just replace `while` with `if` statement. – eugene_che Aug 04 '11 at 15:29
  • Again you are right, but not say I wanted to pull data from row (some number) and column (some number), what could I do, or even to just say read line (some number) and return all values. I can't just change the while loop again. Is this where vectors come in? – Rob Aug 04 '11 at 15:40
  • @Rob, if you need to post-process some data you have to convert and save it into suitable data structure (i.e. vector) while reading the file. Selection of the appropriate data structure depends on you purposes - they have different complexity of operations (i.e. access, add & remove element). – eugene_che Aug 04 '11 at 15:51
1

You may want to change the condition in your while loop:

char separator;
int value1;
int value2;
int value3;
while (myfile >> value1)
{
    // Skip the separator, e.g. comma (',')
    myfile >> separator;

    // Read in next value.
    myfile >> value2;

    // Skip the separator, e.g. comma (',')
    myfile >> separator;

    // Read in next value.
    myfile >> value3;

    // Ignore the newline, as it is still in the buffer.
    myfile.ignore(10000, '\n');

    // Process or store values.
}

The above code fragment is not robust but demonstrates the concept of reading from a file, skipping non-numeric separators and processing the end of the line. The code is optimized either.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154