stream problem

Discussion in 'C++' started by vsgdp, Aug 25, 2003.

  1. vsgdp

    vsgdp Guest

    Hello,

    I'm wondering why this outputs garbage:

    std::eek:fstream outFile("test.txt");

    outFile << 1.0f << std::endl;
    outFile << filename << std::endl;
    outFile << 2.0f << std::endl;
    std::string s;
    float x;
    std::ifstream inFile("test.txt");
    inFile >> x;
    std::cout << x << std::endl;
    std::getline(inFile, s, '\n');
    std::cout << s << std::endl;
    inFile >> x;
    std::cout << x << std::endl;

    and why this works:

    outFile << filename << std::endl;
    outFile << 1.0f << std::endl;
    outFile << 2.0f << std::endl;
    std::string s;
    float x;
    std::ifstream inFile("test.txt");
    std::getline(inFile, s, '\n');
    std::cout << s << std::endl;
    inFile >> x;
    std::cout << x << std::endl;
    inFile >> x;
    std::cout << x << std::endl;

    I think the former doesn't work because the getline() is reading the same
    line as the previous inFile >> read. How can I get it to skip to the next
    line, if that is indeed the problem.
     
    vsgdp, Aug 25, 2003
    #1
    1. Advertising

  2. "vsgdp" <> wrote in message
    news:LMr2b.10463$Qy4.4180@fed1read05...
    > Hello,
    >
    > I'm wondering why this outputs garbage:
    >
    > std::eek:fstream outFile("test.txt");
    >
    > outFile << 1.0f << std::endl;
    > outFile << filename << std::endl;
    > outFile << 2.0f << std::endl;
    > std::string s;
    > float x;
    > std::ifstream inFile("test.txt");
    > inFile >> x;
    > std::cout << x << std::endl;
    > std::getline(inFile, s, '\n');
    > std::cout << s << std::endl;
    > inFile >> x;
    > std::cout << x << std::endl;
    >
    > and why this works:
    >
    > outFile << filename << std::endl;
    > outFile << 1.0f << std::endl;
    > outFile << 2.0f << std::endl;
    > std::string s;
    > float x;
    > std::ifstream inFile("test.txt");
    > std::getline(inFile, s, '\n');
    > std::cout << s << std::endl;
    > inFile >> x;
    > std::cout << x << std::endl;
    > inFile >> x;
    > std::cout << x << std::endl;
    >
    > I think the former doesn't work because the getline() is reading the same
    > line as the previous inFile >> read.


    Not quite.

    getline is doing what it is supposed to which is reading up to the next
    newline. Your mistake is thinking that inFile >> x will read a newline, but
    it doesn't. inFile >> x reads a number and a newline is not part of a
    number. So when you have a number followed by a newline the newline is left
    behind unread. Then getline comes along and reads it.

    > How can I get it to skip to the next
    > line, if that is indeed the problem.
    >


    Use getline twice?

    john
     
    John Harrison, Aug 25, 2003
    #2
    1. Advertising

  3. vsgdp

    Ron Natalie Guest

    "John Harrison" <> wrote in message news:bidnc9$87l3m$-
    > > How can I get it to skip to the next
    > > line, if that is indeed the problem.
    > >

    >
    > Use getline twice?
    >

    or ignore.
     
    Ron Natalie, Aug 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    759
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    639
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    456
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    558
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    399
    James Kanze
    May 10, 2007
Loading...

Share This Page