get lines with c++

This is a discussion on get lines with c++ within the C++ Programming forums, part of the General Programming Boards category; Not sure what to tell you. I was able to read an example file with minimal changes. I never intended ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    57
    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

Page 2 of 2 FirstFirst 12
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, 05:45 PM
  2. Last 2 lines
    By Aliaks in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2009, 01:50 AM
  3. D3D 9 Lines
    By yaya in forum Game Programming
    Replies: 4
    Last Post: 08-28-2009, 01: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, 11:10 PM

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