write to the file and read from file??

Hi
im trying to write records to the file and with other option
read from that file.
i can write to the file but i want to keep the old entry
this codes keep refreshing the file with the new one,
and i cannot read from file.
anyone can help whit this plase?????????
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void writeAppointmenRecords( )
{
	int index;
    //create and open an output text file

       ofstream outfile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::out);
// Check if file is opened
            if(!outfile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
         cout<<"\nPress any key to proceed ";
		 cin.get();                 
	  }
		
	for (index=0; index<currentSize; index++)
	{
        outfile<<AppointmenList[index].name;
		outfile<<setw(10)<<AppointmenList[index].description;
		outfile<<setw(5)<<AppointmenList[index].appdate.day;
		outfile<<"/"<<AppointmenList[index].appdate.mounth;
		outfile<<"/"<<AppointmenList[index].appdate.year;
		outfile<<setw(8)<<AppointmenList[index].time<<endl;
		
	}  
     //option2: write values to file in an 8 characters field.
	
    outfile.close( );                     // close file
    cin.get();

}

void readAppointmenRecords( )
{  //create a stream and open the file 'AppointmenRecords.txt' for input

	ifstream infile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::in);
		// check if file is opened
		  if(!infile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
	                            //indicate program failed
	  cin.get();
	  }
	while (!infile.eof( ))    //eof( ) End Of File function. Returns false if end file reached
       {
		getline(infile, AppointmenList[currentSize].name);
		getline(infile, AppointmenList[currentSize].description);
		infile.get();
		infile>>AppointmenList[currentSize].appdate.day;
		infile>>AppointmenList[currentSize].appdate.mounth;
		infile>>AppointmenList[currentSize].appdate.year;
		getline(infile, AppointmenList[currentSize].time);
        }
    
    infile.close( );           // close file
	cin.get();
    
}
open in append mode otherwise every time a new file will be created and old one overwritten.
is any way to do that with outfile????

why i cant read then

i can write to the file but cannot read?????
because you are using ofstream which can only help in writing..
for both use fstream.

this can read as well write. and when you try to write use ios::append. this will append to the old file.
hi a made cupple of changes on my codes,
i m still having truble with reading can anybody help me with this or
can anyone give me some exaples or something so i can try to see my mistakes
thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void writeAppointmenRecords( )
{
	int index;
    //create and open an output text file

        ofstream outfile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::app);
// Check if file is opened
            if(!outfile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
         cout<<"\nPress any key to proceed ";
		 cin.get();                 
	  }
		
	for (index=0; index<currentSize; index++)
	{
        outfile<<AppointmenList[index].name;
		outfile<<setw(15)<<AppointmenList[index].description;
		outfile<<setw(15)<<AppointmenList[index].appdate.day;
		outfile<<"/"<<AppointmenList[index].appdate.mounth;
		outfile<<"/"<<AppointmenList[index].appdate.year;
		outfile<<setw(15)<<AppointmenList[index].time<<endl;
		
	}  
     //write values to file in an 15 characters field.
	
    outfile.close( );    // close file
    //cin.get();

}

void readAppointmenRecords( )

{  
	currentSize=0;
	//create a stream and open the file 'AppointmenRecords.txt' for input

	 ifstream infile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::in);
		// check if file is opened
		  if(!infile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
	                            //indicate program failed
	  cin.get();
	  }
		  
	while (!infile)    //eof( ) End Of File function. Returns false if end file reached
       {
		getline(infile, AppointmenList[currentSize].name);
		getline(infile, AppointmenList[currentSize].description);
		infile.get();
		infile>>AppointmenList[currentSize].appdate.day;
		infile>>AppointmenList[currentSize].appdate.mounth;
		infile>>AppointmenList[currentSize].appdate.year;
		getline(infile, AppointmenList[currentSize].time);
		currentSize+=1;
        }
    infile.close( ); // close file
	currentSize = currentSize -1;
	cin.get();
}
Topic archived. No new replies allowed.