File Parsing

This is a discussion on File Parsing within the C++ Programming forums, part of the General Programming Boards category; Hello; I am having problems inputting a file and getting it the way I want it. The followin is an ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    File Parsing

    Hello;

    I am having problems inputting a file and getting it the way I want it. The followin is an example of the file


    8
    -1 -1 -1
    1 -1 -1
    1 1 -1
    -1 1 -1
    -1 -1 1
    1 -1 1
    1 1 1
    -1 1 1
    4 0 1 2 3
    4 1 5 6 2
    4 5 4 7 6
    4 4 0 3 7
    4 3 2 6 7
    4 0 4 5 1


    The 8 at the top tells how many of the next set(in lines) there are. The next set, the -1-1-1 and so on are the xyz coordinates. I need to bring these in an store in a vector of structs as below. The final set of numbers are verticies that need to be stored in a vector of vectors. The storing in vectors and structs is not the problem. The problem lies in getting the correct input and converting to floats(set 2) and ints(set3) for storage.

    THe vectors and structs are as follows:
    Code:
    struct coords{
    int x;
    int y;
    int z;
    }
    
    vector<coords> coordinates
    Not sure if how to declare a vector of vectors
    vector<int> store
    vector<store> vec1


    Hope someone can help

    i call the fstream myfile.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    retrieving data from a file stream is as simple as
    Code:
        int i;
        myfile >> i;
    Although you really should perform error checking in case the data retrieved from the file is of the wrong type. (If this happens, the stream will fail/set failbit, and you'll need code to handle that)

    you can also use
    Code:
        string s;
        getline(myfile, s);
    This will store it as plain text, and you'll need an extra step to parse the string to ints/floats. This is the long-winded way



    Code:
    Not sure if how to declare a vector of vectors
    a vector of vectors is declared the same way as a vector of any other type.
    Code:
    vector< vector<int> >
    or, if that looks ugly to you, use a typedef,
    Code:
    typedef vector< vector<int> > vec2D;
    you could add/retrieve elements of your "2D" vector like this,
    Code:
    #include <vector>
    #include <iostream>
    using namespace std;
    
    typedef vector< vector<int> > vec2D;
    
    int main()
    {
        vec2D myData;
        vector<int> vec;
    
        vec.push_back(1);
        myData.push_back(vec);
        myData.at(0).push_back(2);
    
        cout << myData.at(0).at(0) << " "
             << myData.at(0).at(1);
        cin.get();
    }
    This is a bit of an ugly example since it uses "magic" numbers to access the elements, you're better off using iterators for reading the contents, but I hope this gives you the basic idea.
    Last edited by Bench82; 03-29-2006 at 10:12 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Thanks for the info, especially the vector of vectors, but will the myfile >> intx work fo the negative and positive values?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Thanks to all for the help... here is the final working code:

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<vector>
    
    using namespace std;
    struct coord{
           int x;
           int y;
           int z;
    };
    struct vert{
           vector <int> verts;
    };
    vector <coord> cord;
    vector <vert> vec1;
    
    
    int main()
    {
      string line;
      int num_lines;
      int i;
      int x1,y1,z1;
      coord temp;
      
      ifstream myfile ("cube.off");
      if (myfile.is_open())
      {
        myfile >> num_lines;
        cout << "HERE: " << num_lines << endl;                   
        for(i=0;i<num_lines;i++)
        {
          //getline (myfile,line);
          //cout << line << endl;
          myfile >> x1;
          myfile >> y1;
          myfile >> z1;
          temp.x=x1;
          temp.y=y1;
          temp.z=z1;
          cord.push_back(temp);
          cout << "LINE IS: "<<x1<<" "<<y1<<" "<<z1<<endl;
        }
        myfile.close();
      }
      else
      {
           cout << "Unable to open file";
      } 
      
      for(i=0;i<cord.size();i++)
      {
         cout << cord[i].x << endl;                          
                                 
     }
        
    system("PAUSE");    
    return 0;    
    }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by swanley007
    Thanks for the info, especially the vector of vectors, but will the myfile >> intx work fo the negative and positive values?
    You probably already figured this out for yourself, but the answer is yes anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21