Computing.Net > Forums > Programming > why does it loop once too many times?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

why does it loop once too many times?

Reply to Message Icon

Name: rebelswimmer290
Date: August 5, 2009 at 08:29:51 Pacific
OS: Windows XP
Subcategory: C/C++
Comment:

can somebody look at this and tell me why it loops once extra when reading back from the saved files?


void WeatherStation::SaveToFile()
{
fstream OutFile(FileName.c_str(), ios::out);
for(int K = 0 ; K < Station.size() ; K++)
{
OutFile << Station[K] << endl;
OutFile << Agents[K] << endl;
OutFile << Temp[K] << endl;
OutFile << Highs[K] << endl;
OutFile << Lows[K] << endl;
}
OutFile.close();
}

void WeatherStation::ReadFromFile()
{
fstream InFile(FileName.c_str(), ios::in);
string Buffer;
double Bufferint;

while(!InFile.eof()) //Check to see if you're at the end of the file...
{

getline(InFile, Buffer);
cout << "Buffer1 = " << Buffer << endl;
Station.push_back(Buffer);//storing information
getline(InFile, Buffer);
cout << "Buffer2 = " << Buffer << endl;
Agents.push_back(Buffer);
InFile >> Bufferint;
cout << "Buffer3 = " << Bufferint << endl;
Temp.push_back(Bufferint);
InFile >> Bufferint;
cout << "Buffer4 = " << Bufferint << endl;
Highs.push_back(Bufferint);
InFile >> Bufferint;
cout << "Buffer5 = " << Bufferint << endl;
Lows.push_back(Bufferint);
InFile.ignore(100, '\n'); //Flush the buffer after a numeric input...
}



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: August 5, 2009 at 08:57:46 Pacific
Reply:

while(!InFile.eof()) //Check to see if you're at the end of the
file...

This does not do what the comment describes. You don't
know that you are at the end of the file until you actually try to
read past the end and fail.

Compare that with the old Pascal idiom which is similar to
what you have here. In Pascal, when you check for eof(), the
runtime tries to read a character from the file "behind the
scenes" and tells you if it is at the end. If it is not at the end,
it saves that character in a buffer, and returns it next time you
ask to read a character from the file.

C and C++ don't have this look-ahead feature. You need to
recode your function so that it checks the values returned by
getline. (Or check with eof() after calling getline.)


0

Response Number 2
Name: rebelswimmer290
Date: August 5, 2009 at 09:03:34 Pacific
Reply:

can i still use these vector lists to do that?
or do i need to change my whole program?
or do you know if theres a code i can just replace with?
btw.
thanks for replying!


0

Response Number 3
Name: klint
Date: August 5, 2009 at 09:12:38 Pacific
Reply:

What "vector lists"? All you need to change is function
ReadFromFile, I see neither vectors nor lists in there. You can
keep the same local variables. To make the logic easier to
understand, you might want to write an additional function (pass
it the fstream as an argument) that reads one record from the file
and returns a boolean to tell you whether it's succeeded or not.
You can then call the new function in the while loop.


0

Response Number 4
Name: rebelswimmer290
Date: August 5, 2009 at 09:21:39 Pacific
Reply:

so im looking to do something with pascal kind of?


0

Response Number 5
Name: rebelswimmer290
Date: August 5, 2009 at 09:24:54 Pacific
Reply:

im looking at a tutorial that talks about posttesting and while loops which is i think a during loop?
for this function, do you think it would be more efficient/effective to do one during or after?


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: August 5, 2009 at 09:33:02 Pacific
Reply:

Replace:

while(!InFile.eof()) //Check to see if you're at the end of the file...
{

  getline(InFile, Buffer);

With:

while (getline(InFile, Buffer)) {


0

Response Number 7
Name: rebelswimmer290
Date: August 5, 2009 at 09:43:37 Pacific
Reply:

oh thanks so much!
just to make sure i understand what changed is you tested it before instead of waiting till after to see if it was the end?


0

Response Number 8
Name: Razor2.3
Date: August 5, 2009 at 09:56:49 Pacific
Reply:

I moved the getline() into the while. So long as your istream biased object remains in a good state, you'll get back a != 0 number. When the read fails, presumably because you hit the EoF, ios::operator void*() returns 0. This reads as false, and you exit the while loop.


0

Response Number 9
Name: rebelswimmer290
Date: August 5, 2009 at 10:01:20 Pacific
Reply:

that makes sense thanks!


0

Response Number 10
Name: rebelswimmer290
Date: August 5, 2009 at 10:12:55 Pacific
Reply:

is there a way to make it not double up if you type in save again?


0

Response Number 11
Name: Razor2.3
Date: August 5, 2009 at 10:25:27 Pacific
Reply:

is there a way to make it not double up if you type in save again?
What now?


0

Response Number 12
Name: rebelswimmer290
Date: August 5, 2009 at 10:28:04 Pacific
Reply:

i have a program that saves to a file
but when i type in save again
it double its up
etc.
my input is
bob
frank
and when i save once it saves as
bob
frank
but when i save it again it saves double
bob
frank
bob
frank


0

Response Number 13
Name: Razor2.3
Date: August 5, 2009 at 11:09:41 Pacific
Reply:

You're probably opening the output file as append. Instead, try adding the trunc flag.


0

Response Number 14
Name: rebelswimmer290
Date: August 5, 2009 at 19:15:48 Pacific
Reply:

for my save to file i use

fstream OutFile(FileName.c_str(), ios::out);
and for reading back in i use
fstream InFile(FileName.c_str(), ios::in);
i tried putting in the trunc part
but it didnt do anything
thanks for responding though!


0

Response Number 15
Name: Razor2.3
Date: August 5, 2009 at 20:22:18 Pacific
Reply:

If fstream OutFile(FileName.c_str(), ios::out | ios::trunc) doesn't work, your best option would probably be to just delete the file before opening it.

While we're on the subject, you ARE opening it when you do your save function, and then closing it at the end of the function, aren't you?


0

Response Number 16
Name: rebelswimmer290
Date: August 5, 2009 at 20:27:49 Pacific
Reply:

opening and closing as in what?
i put my input it, save the program, read the file i saved, and usually just quit after that.
however if not, i put my input in, save the program, read the file i saved, save the program again, read it again (which is where hte error is) and then quit


0

Response Number 17
Name: Razor2.3
Date: August 5, 2009 at 21:24:13 Pacific
Reply:

Open and close the output file. You ARE doing that in your save function, and not your constructor and destructor, correct?


0

Response Number 18
Name: rebelswimmer290
Date: August 6, 2009 at 09:04:24 Pacific
Reply:

yes
thats in my save file


0

Response Number 19
Name: Razor2.3
Date: August 6, 2009 at 09:37:09 Pacific
Reply:

Then you'll have to delete the file before you (re)open it. This is platform dependent.

On Windows, the code looks like this (be sure to #include <Windows.h>):

if (!::DeleteFile(FileName.c_str())  && ::GetLastError() != ERROR_FILE_NOT_FOUND)
  std::cerr << "Unable to delete file" << std::endl;

On POSIX, the function is unlink(FileName.c_str()), found in #include <unistd.h>


0

Response Number 20
Name: klint
Date: August 6, 2009 at 10:01:13 Pacific
Reply:

You shouldn't have to delete the file, as long as you are open it with ios::out | ios::trunc. In your original code, you were just passing ios::out. You need to pass ios::trunc too.


0

Response Number 21
Name: rebelswimmer290
Date: August 7, 2009 at 06:47:12 Pacific
Reply:

thanks!
you guys were really helpful!!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: why does it loop once too many times?

Problem with recursion www.computing.net/answers/programming/problem-with-recursion/13116.html

Lock Cursor Position www.computing.net/answers/programming/lock-cursor-position/8193.html

Help Plz www.computing.net/answers/programming/help-plz/1770.html