开发者

I need help with parsing from a file in C++

开发者 https://www.devze.com 2023-02-18 22:43 出处:网络
Ok so basically I have this text file with numbers and letters that\'s supposed to represent the vertices of a polygon. This part isn\'t important because I\'m having trouble parsing from the file int

Ok so basically I have this text file with numbers and letters that's supposed to represent the vertices of a polygon. This part isn't important because I'm having trouble parsing from the file into an int. So far the function looks like:

    void parseModel(void)
{
    int num_vertices, num_faces;
    char data[255];
    ifstream ifs("rect-skinny.d");
    ifs.getline(data, 255);
    istrstream ins(data);
    ins >> num_vertices;
    cout << num_vertices << endl;
    ifs.close();
}

I've tried many different methods that all gave me different but incorrect answers. This one outputs the number -858993460 for some reason. Other times it would just output closed brackets when I would try to print data separately. I can't figure out what I'm doing wrong since this seems l开发者_如何转开发ike it should work. The input file is:

 data    8    6
 -0.5   1.0   0.3
 0.5   1.0   0.3
 0.5   -1.0   0.3
 -0.5   -1.0   0.3
 -0.5   1.0   -0.3
 0.5   1.0   -0.3
 0.5   -1.0   -0.3
 -0.5   -1.0   -0.3
   4   1   2   3   4
   4   1   5   6   2
   4   2   6   7   3
   4   5   8   7   6
   4   1   4   8   5
   4   3   7   8   4

Basically all I'm trying to do right now is get the first line and put those numbers int it into num_vertices and num_faces, respectively.


After you read the text string "data" it will contain "data 8 6".

The line ins >> num_vertices; will try to read an integer, but will find "data 8 6" and so will fail. Try something like this:

void parseModel(void)
{
    int num_vertices, num_faces;
    char data[255];
    std::ifstream ifs("rect-skinny.d");
    ifs.getline(data, 255);       // data contains "data    8    6"
    std::istrstream ins(data);    // ins  contains "data    8    6"
    ins.ignore(4, ' ');           // ins  contains "    8    6"
    ins >> num_vertices;
    std::cout << num_vertices << endl;
    ifs.close();
}


This is how I would approach the problem....

If this fails, you may want to just organize your file with a return after each piece of data. Your life will be infinitely easier and your data will be in the array anyway.

If you really have your heart set on using char arrays you could just change the string to a char array.

void parseModel(void)
{
    //will read in heading data first, then parse model data
    //you could alternatively store your header data in a struct
    //or class for better organization

    //work with strings, not raw char arrays
    string data;
    int num_vertices;
    int num_faces;

    //open the stream
    ifstream ifs("rect-skinny.d");
    if(!ifs.is_open())
    {
        cout << "ERROR- ifstream NOT OPEN\n";
    }

    //read in heading data
    ifs >> data;
    ifs >> num_vertices;
    ifs >> num_faces;

    //array of vertex data
    float vertices[num_vertices];
    //array of faces data
    int   faces[num_faces];

    //loop through all the vertices, reading each into the array
    for(int i = 0; i < num_vertices; i++)
    {
        ifs >> vertices[i];
    }
    //do the same for the faces
    for(int n = 0; n < num_faces; n++)
    {
        ifs >> faces[n];
    }
    //closing the stream
    ifs.close();
}


You may want to consider just using fscanf.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号