Thread: get lines with c++

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    87

    get lines with c++

    i want to read my file using just c++ instructions
    so i ake like that
    Code:
    std::ifstream infile;
    
      infile.open ("file.txt", std::ifstream::in);
    now i want to get the line but it does not work when i make like that
    Code:
    char line[1000];
    while (std::istream::getline(line,1000))
         
      
        {
    another thing , i have this program that read the 4 first information (number) from my file
    Code:
      std::ifstream stream("file.txt" );
        
        if (stream)
        {
            
            joint ja;
            pose p;
            stream >>p.time;
            stream >> ja;
            stream >> p;
            
        }
        }
    if in my file thre are
    Code:
    -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 
    .........................
    But it just recuperate those information
    Code:
    -0.01 0.03 0.04 -0.5 0.6
    because i do not use getline

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    std::istream::getline(line,1000)
    The istream::getline part of wherever you looked this function up from is supposed to mean that getline is a member of istream, so you have to call it on cin, or an input file.

    stream >>p.time;
    stream >> ja;
    stream >> p;

    How did you implement these operators? If there is a problem with them, I need to see.
    Last edited by whiteflags; 12-05-2012 at 08:13 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    that's my first code ,that read only the first 4 numbers in the first line
    Code:
    #include <iterator>
    #include <iostream>
    #include <vector>
    #include <fstream>
     
    struct Vector3 
    {
        float x, y, z;
    };
     
    struct  joint_angle
    {
        int count;
    
        Vector3 orient;
    };
     
    struct pose
    {
        float time;
        std::vector<joint_angle> angle;
    };
     
    std::vector<pose> posture;
     //operateur lire temps;
    
    // opérateur pour lire un "joint_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;
    }
     
    // opérateur pour lire un "pose"
    std::istream & operator>> (std::istream & stream, pose & p) 
    {
        // stream >>p.time;
         printf ("p.timedans stre===>%f",p.time);
        //p.time = 0;
        std::copy(std::istream_iterator<joint_angle>(stream), std::istream_iterator<joint_angle>(), std::back_inserter(p.angle));
        return stream;
    }
     
    // pour lire un fichier
    int main()
    {
        std::ifstream stream("angle.txt" );
        
        if (stream)
        {
            // test operator >> joint_angle
            joint_angle ja;
            pose p;
            stream >>p.time;
            stream >> ja;
        
            // test operator >> pose
          //  pose p;
            stream >> p;
            
        }
       
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    hi , no answer ?
    i can't found solution

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    no answers to what? explain how your second post relates to your first one in terms of the error / problem you are having.
    file io is very simple in C++, your code makes it look complicated, also you are mixing c and c++ in both examples, why?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    hi again ,
    the code in my seconde reply work BUT not as i want

    my file is like that
    -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

    .........................

    i want to read it and put the result in struct
    for example
    -0.01 is pose[0].time , 0.03 0.04 -0.5 is pose[0].joint_angle[0]........
    -0.03 is pose[1].time ,0.05 0.06 -0.005 is pose[1].joint_angle[0]...

    BUT the code read just the 4 numbers in the first line (-0.01 0.03 0.04 -0.5) and stop reading

    that's the problem of the code

    Quote Originally Posted by rogster001 View Post
    also you are mixing c and c++ in both examples, why?
    there is C interuction in the code ?.
    Last edited by dreamvig; 12-08-2012 at 02:19 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dreamvig View Post
    there is C interuction in the code ?.
    Yes.
    printf is C.
    In C++, you would use std::cout.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    printf is C.
    i jus ad it to see if it work or no, so i can use cout too
    it's not here the problem

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point is just - avoid printf. It's not safe, it's bothersome to use and easy to use incorrectly.
    Otherwise you are going to hear the same things again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you want to read each value e.g. 1.0 from each line into a member of your struct right?

    then you need to add more work - get the line in your stream reading - parse the line for the tokens - convert the token if you require numerical data, store the resulting value in a member of your struct .

    I am not reading your code right now , but the usual getline(...) call will do exactly what it says - once you have that line stored, then you can use other means to break out the values
    Last edited by rogster001; 12-08-2012 at 03:09 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Hi
    ou want to read each value e.g. 1.0 from each line into a member of your struct right?
    yes ,
    yes , each line is in pose struct
    Code:
    struct pose {     float time;     std::vector<joint_angle> angle; };
    where the first number in the line is an "time " and the other is in joint_angle struct
    then you need to add more work - get the line in your stream reading -
    yes i tried to make that , but first i do ot know the number of the information in the line , i just know that the first number is an float.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    yes i tried to make that , but first i do ot know the number of the information in the line , i just know that the first number is an float.
    I'm not sure I'm appreciating the problem.

    You seemed to indicate that you knew how the file was organized earlier.
    -0.01 is pose[0].time , 0.03 0.04 -0.5 is pose[0].joint_angle[0]........
    -0.03 is pose[1].time ,0.05 0.06 -0.005 is pose[1].joint_angle[0]...
    I think rogsters point is that you should use string parsing instead of reading directly from the file, and from your earlier posts there shouldn't be a problem with this. A getline that reads up to 1000 characters is one that reads a *lot* of potential data.

    What I can definitely tell you with this is that you need code to read each joint angle. Simple code like
    stream >> ja;
    can work. As long as you focus on reading one angle at a time.

    I think you can do something such as
    Code:
    #include <iomanip>
    // for noskipws
    
    // 1. read time information
    
    // 2. read pose information
    joint_angle ja;
    vector<joint_angle> angles;
    char c;
    while ( stream >> ja >>  noskipws >> c ) {
       angles.push_back(ja);
       if (c == '\n' ) {
          // new pose incoming
       }
    }
    This would correctly read all the joint angles for one pose until you move to the next line for the next pose if you implemented them this way. I think an approach like this is simpler than trying to use the STL algorithms straight-off. The STL can be unhelpful... if you don't know how to read your files.
    Last edited by whiteflags; 12-08-2012 at 04:42 PM.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Hi, thanks for your help
    You seemed to indicate that you knew how the file was organized earlier.
    yes , i know , the file is an float numbers where the first number is "time" and every next three numbers are joint_angle
    I think rogsters point is that you should use string parsing instead of reading directly from the file
    you mean here that i must read string and i pars it into float insread to read directely the number as float ?
    What I can definitely tell you with this is that you need code to read each joint angle. Simple code like
    stream >> ja;
    that code existed
    Code:
    std::istream & operator>> (std::istream & stream, joint_angle & ja) 
    {
        
        stream >> ja.orient.x >> ja.orient.y >> ja.orient.z;
           return stream;
    }
    noskipws >> c
    what you mean by "noskipws "?
    if (c == '\n' )
    here i use that line instead getline ?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    you mean here that i must read string and i pars it into float insread to read directely the number as float ?
    I'm not saying you "must" do anything. When you originally posted the thread your question was centered around istream::getline This is why I'm getting confused. Consider the code that started the thread. I will post corrected code below. It's a little program that simply reads lines in the file and spits them back out.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void simpleRead();
    
    int main() {
    
        simpleRead();
    
        return 0;
    
    }
    
    void simpleRead() {
        std::ifstream file;
    
        file.open("file.txt"); // This object assumes you want to read the file
    
        if (file.is_open()) {
            char lineContent[1000];
    
            while (file.getline(lineContent, 1000)) {
    
                cout << lineContent << endl;
    
            }
    
            file.close();
        }
    }
    That's about as simple as I can make it without changing too much. std::string would be better to use here, though. Anyway ll of the string parsing you will have to do should take place in between lines 24 and 28.

    You can in fact feed lineContent into a std::stringstream and treat that exactly like your file content. This is string parsing. It would make error handling easier for you. If the user makes a mistake in the file, on a particular line, you can write an error notice to cout and continue reading the file, for example. Either way, you need to decide if you want to read directly from the file or not. The two ways are mutually exclusive and you will end up with very different code either way.

    The part that made me the most confused was this code.
    Code:
    std::istream & operator>> (std::istream & stream, pose & p)
    {
        // stream >>p.time;
         printf ("p.timedans stre===>%f",p.time);
        //p.time = 0;
        std::copy(std::istream_iterator<joint_angle>(stream), std::istream_iterator<joint_angle>(), std::back_inserter(p.angle));
        return stream;
    }
    I don't think you're doing this correctly. Did you test this? Soldiering on, I offered you an alternative method of parsing your file, which I felt was simpler and I will now explain.
    Code:
     
    // 1. read time information
     
    // 2. read pose information
    joint_angle ja;
    vector<joint_angle> angles;
    char c;
    while ( stream >> ja >>  noskipws >> c ) {
       angles.push_back(ja);
       if (c == '\n' ) {
          stream >> skipws; // Reset - because of the reading we have to repeat (see 1)
          // new pose incoming
          // possibly break; here
       }
       
    }
    It's a bit rough around the edges

    I left out part one under the assumption that you knew what that step entailed.

    Part two entailed getting all the angles in a particular pose. The noskipws modifier simply makes sure that the character c is given the value '\n' (which is a newline char) eventually. Please read the information on this page. If we didn't use it, we would end up in trouble, because white space is normally skipped and we depend on the information from newline to move to the next pose. This is in line with your thinking here:
    -0.01 is pose[0].time , 0.03 0.04 -0.5 is pose[0].joint_angle[0]........
    -0.03 is pose[1].time ,0.05 0.06 -0.005 is pose[1].joint_angle[0]...
    After you do all the reading for one pose, you can add everything (the time information and the vector of joint_angle, wholesale) into a larger data structure. The good news is that you can use my way of reading joint angles whether you choose to do string parsing or not. The only things that would change is the type of stream, and the fact that you won't need to mess with the white space anymore.

    The only way to make it any simpler would be to know the number of poses and joint angles possible, in advance, I think. Good luck.
    Last edited by whiteflags; 12-09-2012 at 01:14 PM.

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    hi again,
    i am sorry for my questions i know that they are trivial for you but it's important for me to ask questions to understand good

    if i use your code like that
    Code:
     std::ifstream stream("angle.txt" );
    joint_angle ja;
    std::vector<joint_angle> angles;
    char c;
    while ( stream >> ja >>  std::noskipws >> c ) {
          angles.push_back(ja);
       if (c == '\n' ) {
          stream >> std::skipws; // Reset - because of the reading we have to repeat (see 1)
          // new pose incoming
          // possibly break; here
       }
        
    }
    it read just the four number in the first line
    so as i see the problem is here
    Code:
    std::ifstream stream("angle.txt" );

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