stream problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vsgdp

    stream problem

    Hello,

    I'm wondering why this outputs garbage:

    std::ofstream outFile("test.t xt");

    outFile << 1.0f << std::endl;
    outFile << filename << std::endl;
    outFile << 2.0f << std::endl;
    std::string s;
    float x;
    std::ifstream inFile("test.tx t");
    inFile >> x;
    std::cout << x << std::endl;
    std::getline(in File, 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.tx t");
    std::getline(in File, 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.


  • John Harrison

    #2
    Re: stream problem


    "vsgdp" <nospam@nospam. com> wrote in message
    news:LMr2b.1046 3$Qy4.4180@fed1 read05...[color=blue]
    > Hello,
    >
    > I'm wondering why this outputs garbage:
    >
    > std::ofstream outFile("test.t xt");
    >
    > outFile << 1.0f << std::endl;
    > outFile << filename << std::endl;
    > outFile << 2.0f << std::endl;
    > std::string s;
    > float x;
    > std::ifstream inFile("test.tx t");
    > inFile >> x;
    > std::cout << x << std::endl;
    > std::getline(in File, 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.tx t");
    > std::getline(in File, 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.[/color]

    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.
    [color=blue]
    > How can I get it to skip to the next
    > line, if that is indeed the problem.
    >[/color]

    Use getline twice?

    john


    Comment

    • Ron Natalie

      #3
      Re: stream problem


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:bidnc9$87l 3m$1@ID-196037.news.uni-[color=blue][color=green]
      > > How can I get it to skip to the next
      > > line, if that is indeed the problem.
      > >[/color]
      >
      > Use getline twice?
      >[/color]
      or ignore.


      Comment

      Working...