Thread: get lines with c++

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not sure what to tell you. I was able to read an example file with minimal changes. I never intended to write anything you could just insert into your homework. This is a simple program that reads one pose at a time and spits it back out.

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    struct Vector3 {
        float x, y, z;
    };
    
    struct  joint_angle {
        int count;
    
        Vector3 orient;
    };
    
    struct pose {
        float time;
        vector<joint_angle> angle;
    };
    
    std::istream & operator>> (std::istream & stream, joint_angle & ja)
    {
        ja.count = 0;
        stream >> ja.orient.x >> ja.orient.y >> ja.orient.z;
        /*printf ("ja.orient.x===>%f",ja.orient.x);
        printf ("ja.orient.y===>%f",ja.orient.y);
        printf ("ja.orient.z===>%f",ja.orient.z);*/
        return stream;
    }
    
    int simpleRead(ifstream &stream, pose &p);
    
    int main()
    {
        ifstream file("file.txt");
        pose p;
    
        while (simpleRead(file, p)) {
            cout << p.time;
            for (int i = 0; i < p.angle.size(); i++) {
                cout << " " << p.angle[i].orient.x << " " << p.angle[i].orient.y << " " << p.angle[i].orient.z;
            }
            cout << "\n";
        }
        return 0;
    
    }
    
    int simpleRead(ifstream &stream, pose &p)
    {
        // 1. Read time information
        stream >> p.time;
    
        if (stream.eof()) return 0;
    
        // 2. Read joint angle information
        joint_angle ja;
        vector<joint_angle> angles;
        char c;
        while ( stream >> skipws >> ja >> noskipws >> c ) {
            angles.push_back(ja);
            if (c == '\n' ) {
                break;
            }
        }
    
        p.angle = angles;
        return 1;
    }
    
    /* in file.txt:
    
    -0.01 0.03 0.04 -0.5 0.6 0.8 0.3 -0.005 -0.003 -0.008
    -0.03 0.05 0.06 -0.005  -0.2 0.3 0.8 1.02 1.03 1.04
    */
    Works for me. My assumptions were that there were only one pose per line and that one joint angle was three floats. A pose was made up of multiple angles.

    I do feel like I've been tricked into doing all the hard work... but maybe this will help you understand what I'm trying to say.

  2. #17
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Hi thanks for the code
    and i am so sorry for my questions.

    NB: i found that the problem is in the file.txt , i did not see that each line start by ' ' not with number , for that it made to me problem in cout

    thanks another times

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Translate a string matrix[lines][4] into one[lines][2]
    By muacsom in forum C Programming
    Replies: 3
    Last Post: 06-16-2012, 04:45 PM
  2. Last 2 lines
    By Aliaks in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2009, 12:50 AM
  3. D3D 9 Lines
    By yaya in forum Game Programming
    Replies: 4
    Last Post: 08-28-2009, 12:14 AM
  4. New lines
    By 6arredja in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2004, 01:32 PM
  5. red lines
    By aaaaa in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2003, 10:10 PM