Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   stream problem (http://www.velocityreviews.com/forums/t277062-stream-problem.html)

vsgdp 08-25-2003 05:34 PM

stream problem
 
Hello,

I'm wondering why this outputs garbage:

std::ofstream 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.



John Harrison 08-25-2003 07:17 PM

Re: stream problem
 

"vsgdp" <nospam@nospam.com> wrote in message
news:LMr2b.10463$Qy4.4180@fed1read05...
> Hello,
>
> I'm wondering why this outputs garbage:
>
> std::ofstream 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



Ron Natalie 08-25-2003 07:19 PM

Re: stream problem
 

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

>
> Use getline twice?
>

or ignore.




All times are GMT. The time now is 12:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57