C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 18th, 2003, 11:16 AM
virtue virtue is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 1 virtue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question c++ file i/o question

If I have a method function that saves all my private data to a file in a class I created. And in main I have a loop that executes that “save method function” for as many objects that I happen to create, the first call to “save method” in the loop executes correctly and saves data to the file, but the next time the loop calls “save method” how do I (in the save method function) save the data after the previous data already saved in the file? Is there a function to test what is in a file or how many characters are in a file? Any feedback would be great.

Reply With Quote
  #2  
Old April 18th, 2003, 04:42 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
It sounds like you may be reopening the file everytime and overwriting what's in there. I would suggest you only open the file once, and then writing to it will continue where you left off. Or, if you have to keep opening the file everytime, make sure you open it to append to the file:

const char* filename = "C:\\TestData\\input.txt";
ofstream outFile(filename, ios::out | ios::app);

When you do this:

ofstream outFile(filename);

the default mode is:

ios::out | ios::trunc

which opens the file for output and overwrites anything in the file.

Last edited by 7stud : April 18th, 2003 at 04:49 PM.

Reply With Quote
  #3  
Old April 18th, 2003, 04:58 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
i interpreted it as wanting to first check if there is data in the file, and if there is then append the newest data to the end of the previous segment. in your saveData() method, u will want to have some sort of check to see if there is data in the file, and where this data ends. is your file sequential or random?

edit: just to add a little to 7stud's, if in fact you are wanting to append data, all u have to do is use: ios::ate and it will automatically move to the end of the file.

Last edited by infamous41md : April 18th, 2003 at 05:01 PM.

Reply With Quote
  #4  
Old April 19th, 2003, 04:55 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"i interpreted it as wanting"

I make of the post much couldn't sense .

Reply With Quote
  #5  
Old April 19th, 2003, 11:05 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
^^LOL sense not much made. thinks him must be we psychic.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > c++ file i/o question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway