Results 1 to 8 of 8

Thread: [c++] how can i read a line that contains different data types from a file.

  1. #1
    Join Date
    Oct 2006
    Beans
    323

    [c++] how can i read a line that contains different data types from a file.

    IN c++

    What would be the code if i want to read a line from a file and this line contains numbers of different data types.I want to be able to save each number in a variabe of the same data type.

    Example:

    1.24 345 3442

  2. #2
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [c++] how can i read a line that contains different data types from a file.

    http://www.cplusplus.com/doc/tutorial/files/

    Code:
    ifstream inf;
    double d;
    int n, m;
    
    inf.open("foo.txt", ios::in);
    
    inf >> d >> n >> m;
    
    inf.close();
    Last edited by schauerlich; March 19th, 2010 at 07:37 PM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  3. #3
    Join Date
    May 2007
    Location
    Santiago, Chile
    Beans
    Hidden!
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [c++] how can i read a line that contains different data types from a file.

    Maybe somthing like:
    Code:
    cin >> myfloat >> myint1 >> myint2

  4. #4
    Join Date
    Oct 2006
    Beans
    323

    Re: [c++] how can i read a line that contains different data types from a file.

    Quote Originally Posted by MindSz View Post
    Maybe somthing like:
    Code:
    cin >> myfloat >> myint1 >> myint2
    That was what i was thinking but i wasn't sure.

    Does c++ has this feature to find different data types in a line?

  5. #5
    Join Date
    Oct 2006
    Beans
    323

    Re: [c++] how can i read a line that contains different data types from a file.

    Quote Originally Posted by schauerlich View Post
    I have already checked that and it didn't help

    thanks for replying anyway

  6. #6
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: [c++] how can i read a line that contains different data types from a file.

    what and how the formated operators (<< >>) read is only determined by the type where it should store or output the data. So put a double type on the right side of it and it will read/write doubles

    so to read them all into the same type is easy for doubles because an integer is also a valid double representation for streams

    double a[3]
    somefilestream >> a[0] >> a[1] >> a[2]; // read in any number into doubles
    it will also work for other number formats like 3.23e10

    the other way round is not valid, if inputed 1.2 when reading ints will read the one and stop at the dot. subsequent integer reads will thus fail when the .2 is not removed from the stream first
    Last edited by MadCow108; March 19th, 2010 at 07:49 PM.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,225
    Distro
    Kubuntu

    Re: [c++] how can i read a line that contains different data types from a file.

    After performing the read operations on the stream, verify that all went well with the good() method (or conversely, with fail()).

    Code:
    double d;
    int m, n;
    
    mystream >> d >> m >> n;
    
    if (!mystream.good())
    {
       // error parsing data
    }

  8. #8
    Join Date
    Oct 2006
    Beans
    323

    Re: [c++] how can i read a line that contains different data types from a file.

    Quote Originally Posted by dwhitney67 View Post
    After performing the read operations on the stream, verify that all went well with the good() method (or conversely, with fail()).

    Code:
    double d;
    int m, n;
    
    mystream >> d >> m >> n;
    
    if (!mystream.good())
    {
       // error parsing data
    }
    ok thanks i will try it

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •