0

The text file provided has an undetermined number of lines, each line containing 3 doubles separated by commas. For example:

-0.30895,0.35076,-0.88403

-0.38774,0.36936,-0.84453

-0.44076,0.34096,-0.83035

...

I want to read this data from the file line by line and then split it on the comma(,) sign and save it in an N by 3 array, let's call it Vertices [N] [3], where N designates the undefined number of lines in the file.

My code so far:

void display() {
string line;
ifstream myfile ("File.txt");
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
    // I think the I should do 2 for loops here to fill the array as expected
    }
    myfile.close();

}
else cout << "Unable to open file";

}

The Problem: I managed to open the file and read line by line, but I have no idea how to pass the values into the requested array. Thank you.

EDIT: I have tried modifying my code according to the suggestions i received to the following:

void display() {
string line;
ifstream classFile ("File.txt");
vector<string> classData;
if (classFile.is_open())
{
    std::string line;
    while(std::getline(classFile, line)) {
        std::istringstream s(line);
        std::string field;
        while (getline(s, field,',')) {
            classData.push_back(line);
        }
    }

    classFile.close();

}
else cout << "Unable to open file";

}

Is this the correct? and how can i access each field of the vector i created? (like in an array for example)? I also noticed that these are of type string, how can i convert them to type float? Thank you (:

abedzantout
  • 802
  • 4
  • 15
  • 28
  • 1
    Have a look at http://stackoverflow.com/questions/19936483/c-reading-csv-file/19936571#19936571 –  Mar 31 '15 at 19:29
  • If there are an undetermined number of lines, you should be using `std::vector`, not an ordinary 2d array, to store the data. – PaulMcKenzie Mar 31 '15 at 19:34
  • @DieterLücking I am fairly new to C++, in the answer you provided in the link, I should do 2 while loops? or did I misunderstand somewhere? – abedzantout Mar 31 '15 at 19:43
  • Yes/No - but if you have a fixed width of columns you can use a while/for (and ensure data consistency) –  Mar 31 '15 at 19:47
  • @DieterLücking I have edited my question above according to your suggestions, is this correct? – abedzantout Mar 31 '15 at 20:01
  • Since you want floats: http://stackoverflow.com/questions/1012571/stdstring-to-float-or-double – LogicStuff Mar 31 '15 at 20:02
  • Or read from `std::istringstream` as `float`, `char`, `float`, `char`, `float`. – LogicStuff Mar 31 '15 at 20:04
  • possible duplicate of [Splitting a line of a csv file into a std::vector?](http://stackoverflow.com/questions/11310947/splitting-a-line-of-a-csv-file-into-a-stdvector) – m0nhawk Apr 02 '15 at 10:16

2 Answers2

0

There are many ways to approach this problem. Personally, I would implement a linked-list to save each line read out of the file in its own memory buffer. Once the entire file was read out, I would know how many lines were in the file and process each line in the list using strtok and strtod to convert the values.

Here's some pseudo code to get you rolling:

// Read the lines from the file and store them in a list
while ( getline (myfile,line) )
{
    ListObj.Add( line );
}

// Allocate memory for your two-dimensional array
float **Vertices = (float **)malloc( ListObj.Count() * 3 * sizeof(float) );

// Read each line from the list, convert its values to floats
//  and store the values in your array
int i = j = 0;
while ( line = ListObj.Remove() )
{
    sVal = strtok( line, ",\r\n" );
    fVal = (float)strtod( sVal, &pStop );
    Verticies[i][j++] = fVal;

    sVal = strtok( sVal + strlen(sVal) + 1, ",\r\n" );
    fVal = (float)strtod( sVal, &pStop );
    Verticies[i][j++] = fVal;

    sVal = strtok( sVal + strlen(sVal) + 1, ",\r\n" );
    fVal = (float)strtod( sVal, &pStop );
    Verticies[i][j] = fVal;

    i++;
    j = 0;
}
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
0

The code after edit is right.You can access a vector value in c++ just like you access the values in a normal c++ array.Like classdata[i]You can find more here . Vector reference

And as for your question about converting string to float.In c++ you can directly do this by using stof ie stof(-0.883) you can find reference again here string to float

Best of luck and hope this helps :)