add more records in sequential text file

i can only write 1 record on text file, i want to add another record

Page 1 of 1

2 Replies - 2447 Views - Last Post: 16 October 2009 - 12:52 PM Rate Topic: -----

#1 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

add more records in sequential text file

Posted 16 October 2009 - 09:03 AM

hi guys! i have here a write code on how to write a record on a textfile. But everytime im going to write a new record, it will erase the old record and writes the new record on the text file. I dont know how to add a new record so the old record would not be overwritten or deleted. Can anyone help me with this please? tnx!

#include<iostream>
using std::cin;
 using std::cout;
 using std::endl;
 using std::ios;
 using std::cerr;

 #include<fstream>
 using std::ofstream;

 #include<cstdlib>
 //using std::exit;

 int main()
		 
 {
	ofstream outClientFile("clients.txt", ios::out);
	if (!outClientFile)

	{
		cerr<<"File name could not be opened"<<endl;
		exit(1);

	}
	int account;
	char fname[30];
   

		cout<<"Enter the account number."<<endl;
		cin>>account;
		cout<<"Enter your first name."<<endl;
		cin>>fname;
	{

		outClientFile<<account<< ' ' <<fname<<endl;
		cout<<"?";

	}
	return 0;
 }



Is This A Good Question/Topic? 0
  • +

Replies To: add more records in sequential text file

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: add more records in sequential text file

Posted 16 October 2009 - 12:47 PM

Among other things you can change the mode when
opening the file. Here we can use the append flag.

 int main()
		 
 {
	ofstream outClientFile("clients.txt", ios::out | ios::app);

	// ...

	outClientFile.close();
	return 0;
 }


:magic:
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: add more records in sequential text file

Posted 16 October 2009 - 12:52 PM

ios::out

try this
ios::app

this will append data to the file. you might want to take a new line also :)

just add a endl;

damn beat me too it lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1