Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 02-12-2005, 02:03 AM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
c++ problem: overwriting a file

I'm ouputting the contents of a linked list to a file. It works when there is no existing file. However, when a file already exists with valid content, I can import the data to a linked list, I can modify and display the contents which correctly amends the list but it won't write the modified list to file.

Code:
void write_list(ofstream &data, To_Do_Item_Ptr head) { To_Do_Item_Ptr temp = head; while (head != NULL) { data << head -> desc << endl; temp = temp -> next; if (temp != NULL && (head -> priority > temp -> priority)) { data << endl; } head = head -> next; } data.close(); }

Tracing through the while loop, it iterates the correct number of times so it looks like the data is being written to file but no such luck...

Karim is offline   Reply With Quote
Old 02-12-2005, 02:16 AM   #2
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
It turns out data was never actually opened unless it didn't exist in the directory... I thought another function had already taken care of it.

Cool...

Last edited by Karim : 02-12-2005 at 02:17 AM.
Karim is offline   Reply With Quote
Old 02-12-2005, 04:05 AM   #3
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Hmm...

adding data.open("filename") enables me to overwrite to an existing file but removes the ability to create a file when none exists...
Karim is offline   Reply With Quote
Old 02-12-2005, 04:51 AM   #4
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
quadruple dola.. that's a first...

It turns out if the file existed, the output stream was never opened and vice versa.

I'm not sure how I fixed it but it works now...

For those more skilled than me, isn't it better (safer) to use a fstream when reading/writing to the same file? In this case they were separated because all the reading was done first, stored in a list, and then written to file.
Karim is offline   Reply With Quote
Old 02-12-2005, 03:23 PM   #5
gstelmack
Pro Starter
 
Join Date: Oct 2000
Location: Cary, NC
This all depends on how you were opening "data". You can specify modifiers that determine if you overwrite the file, append to the file, etc. Check the working directory as well, maybe you have extra files elsewhere now...
__________________
-- Greg
-- Author of various FOF utilities
gstelmack is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 05:13 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.