LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2008, 08:05 AM   #1
Jayfrin
LQ Newbie
 
Registered: Apr 2008
Posts: 19

Rep: Reputation: 0
Borland C++ Saving things to txt files


I'm using Borland C++ on windows and I was wondering how I could save things, I have composed code to save one thing to a txt file but everytime I try to add another thing it just overwrites, I was wondering how I could use vectors to save multiple things to a text file then have the program load them all.

This is the composition so far:
Code:
{
        cout <<"your current age is"<< endl;
        ifstream in("Agesave.txt");
        in >> currentage;
        in.close();
        cout <<"****"<< currentage <<"****"<< endl;
        cout <<"Enter your age"<< endl;
        cin >> age;
        ofstream save("Agesave.txt");
        save << age;
        save.close();
        return 0;
}
This is excluding the preprocessors and variable declarations, it works fine and saves and loads 1 number how can I make it load multiple numbers.

Any help would be nice, thanks,
Cheers
 
Old 04-14-2008, 08:44 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
I don’t understand the question. Is it simply that you want to open your output stream in “append” mode instead of truncating?
 
Old 04-15-2008, 08:22 AM   #3
Jayfrin
LQ Newbie
 
Registered: Apr 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I'm sorry, I am bad with words, but what that code does is it saves the imputed number into the .txt file (say number 20) then ends. Then once the program is run again if I save another number to the same .txt file (30), the previously entered number(20) gets replaced with the new one (30), and only one number will stay there. Does that makes more sense?

I do know it is a language problem maybe the "save"

Last edited by Jayfrin; 04-15-2008 at 08:24 AM.
 
Old 04-15-2008, 08:39 AM   #4
Jayfrin
LQ Newbie
 
Registered: Apr 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Yes the append was the problem.
 
Old 04-15-2008, 01:31 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Jayfrin View Post
Yes the append was the problem.
So you can open your ofstream like this instead:
Code:
std::ofstream save("Agesave.txt", ios_base::out | ios_base::app);
to get “append” output mode.
 
  


Reply

Tags
c++



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
working with txt files graziano1968 Linux - General 2 01-22-2008 05:17 AM
changing words in txt files Raakh Linux - Newbie 3 11-15-2007 02:40 AM
Saving Things to Linux Desktops? dav290948 Linux - Desktop 9 03-25-2007 07:15 PM
automatically edit txt files quarry_06 Programming 5 04-04-2006 02:19 PM
I can only print ps/txt files ? TroelsSmit Linux - Newbie 3 09-12-2004 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration