![]() |
| ||
| While loop not ending when reading from file I have a program that reads from a file, but for some reason it never reaches the end of the file or something, because the loop never ends. I put only 2 lines of text in the file, and a loop like this still ran infinite:
Here is the full code: Implementation: //-include the DebitCard implementation You probably don't need to see the header file I created, but if you need to, then let me know. |
| ||
| Re: While loop not ending when reading from file >while (! my_file.eof()) That's wrong. The eof member function wasn't designed to be used as a loop condition. Change it to this: while (my_file.getline(info,sizeof(info)))Aside from that, there's nothing wrong with your code. It works perfectly on three of my compilers. Trim the code down as much as you can such that the problem still exists. Then give us the trimmed code, a sample file that causes the problem, and details about your OS and compiler. Haven't I told you that before concerning the exact same problem? |
| ||
| Re: While loop not ending when reading from file No, this was a different problem. I'm using an older version of bloodshed and it as some really weird lock on files, so once I switched over it worked fine. |
| ||
| Re: While loop not ending when reading from file One other thing, if ur using c++ u should try using strings instead of chars - it shud b familiar coming from a java background And try to avoid !EOF at all costs when reading files, u'll thank me 4 it later. :rolleyes: |
| ||
| Re: While loop not ending when reading from file Quote:
Quote:
|
| ||
| Re: While loop not ending when reading from file >The strod() wouldn't work with strings. Sure it does: strtod ( s.c_str(), 0 );I wouldn't recommend using anything but a null pointer for the second argument though. The C-style string produced by c_str() is transient and it's far too easy to invalidate any pointers to it, so you're better off not even trying unless you can quote relevant parts of the standard from memory. :) A better option would be stringstreams: #include <iostream>>You and Narue both warned me, but I have no clue why... The eof flag for a stream is set only after you've tried and failed to read from the stream. So let's say you use this code to read from a file: #include <iostream>And the contents of the file are: ThisWell, the first three strings are read and printed, just like you expect. However, when getline reads "test", you would expect the loop to stop because it's the last string, right? But it doesn't because getline didn't fail with an end-of-file error, it terminated successfully by reading a line. So the loop continues one more time. This is a classic off-by-one error, and it can be especially frustrating when you end up processing the last record in a file twice. The solution is to use getline's return value as the condition for the loop. That way when getline does fail, it stops the loop at the right time because if getline fails on end-of-file (or an error), execution won't already be in the body of the loop: #include <iostream>Now, you can use eof() (or feof()) for a loop condition if you carefully add a kludge to the loop that protects you from the bug, but that's extra work for no real gain. |
| ||
| Re: While loop not ending when reading from file Quote:
And I find it difficult to imagine that the string class cannot convert to double, so much so that u have to use chars? Do sum research and i'm sure u will find sumthing good about converting strings to ints and doubles :lol: |
| All times are GMT -4. The time now is 10:49 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC