简体   繁体   中英

How can i pass comma separated values into a multidimensional array?

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 (:

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;
}

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM