Clock Crew

Community => General Discussion => Topic farted by: buttplug on November 12, 2009, 01:27:00 AM

Title: Throw me a bone, CC (C++ help)
Post by: buttplug on November 12, 2009, 01:27:00 AM
Can you see any reason as to why this would not output to a file?
void standings::saveTeams(){
file.open(fileHandle);
if (file.is_open()){cout << &quot;FILE OPENED&quot;;}
for(j = begin(); j != end(); j++){
file << setw(16) << (*j).name << setw(8)
<< (*j).wins << setw(8)
<< (*j).losses << setw(8)
<< (*j).percentage << setw(8)
<< (*j).gamesBehind << ' '
<< (*j).lastWins << ' '
<< (*j).lastLosses << ' '
<< (*j).lastStreak << ' '
<< (*j).lastStreakAmmount << ' '
<< (*j).homeWins << ' '
<< (*j).homeLosses << ' '
<< (*j).awayWins << ' '
<< (*j).awayLosses << ' '
<< (*j).interWins << ' '
<< (*j).interLosses << ' '
<< (*j).division << '\n';
}
file.close();
}
This is a member function of class standings which inherits the STL list.

"file" is an object of type fstream, so it should be able to output to the file. The check I have above my loop (if (file.is_open()){cout << "FILE OPENED";}) says the file is indeed open.

j is an iterator to a list in the STL library, if you don't know what it is just think of it as a pointer to a struct holding all the members I am outputting. Yes, the lis is populated, and j is pointing to a populated node in the list. I can cout the members I want to write to the file, and can also look at them in the debugger and see they have values.

fileHandle is the path to the file as a string, right now it is set to "data.txt". It does not change if the full file path is substituted. Yes, the file is in the correct directory.

I fucking hate streams. They always end up frustrating me like this. I figure the problem must be something like it can't open/find the file,. Would it help to overload the << operator for my struct that is the datatype for the list node? I don't know I sat here for 3 hours trying to figure this one thing out.

Don't move this to the Programming forum as it is barren, and I need maximum exposure as this project is past due.

TL;DR, why won't this code output to a file?
Title: Throw me a bone, CC (C++ help)
Post by: buttplug on November 12, 2009, 01:48:05 AM
Quote from: clammo;1691288ofstream file;
file.open(.....etc

I'm using fstream, not ofstream. I asked my professor about this before, but he said it can do both input and output. I will try separating it into ifstream and ofstream and see what happens.

EDIT: It worked. Thanks for the help : )

I wonder why it worked for getting input from the file though?  fstream inherits iostream. But whatever, I want to punch my prof. in the face for saying it should work and subsequently spending so much time on it.
Title: Throw me a bone, CC (C++ help)
Post by: DrClock on November 12, 2009, 02:08:16 AM
well theres your problem


(User was banned for this post; Reason: here's your problem.)