Return to the BobbyGriggs.com C++ Nook


Chapter 7 - First append to text file program!
© 1998 by Bobby Griggs.





// Program name:  Append to file



// Purpose:  To add entries at the end of the file.







// Win32 console application







#pragma hdrstop







#include < iostream.h >



#include < stdio.h>



#include < fstream.h >



#include < assert.h >



#include < string.h >







void Display_current();



void Append_to_file();







#define FILENAME "names.dat"







void main()



{



	Display_current();



	Append_to_file();



	return;



}







void Display_current()



{



	char name[30];



	ifstream in_name;







	in_name.open(FILENAME);



	assert(!in_name.fail());



	while (!in_name.eof())



	{



		in_name.getline(name,sizeof(name));



		cout << name << endl;



	}



	in_name.close();



	assert(!in_name.fail());



	cout << endl;



	return;



}







void Append_to_file()



{



	char name[30],compare[30];



	ofstream name_file;



	



	name_file.open(FILENAME,ios::app);



	assert(!name_file.fail());



	strcpy(compare," ");



	cout << "Enter EXIT to quit ... " << endl << endl;



	while (strcmp(compare,"EXIT"))



	{	



		cout << "Enter name:" << endl;



		gets(name);



		strcpy(compare,name);



		_strupr(compare);



		if (strcmp(compare,"EXIT"))



			name_file << name << endl;



	}



	name_file.close();



	assert(!name_file.fail());



	cout << endl;



	return;



}