Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was learning how to use the fstream class and basically reading and writing data from a file to a file. But now here is the problem. Everytime i create a new record, my old record gets erased. I want to know how i can keep my original record and still add more record into the file. This is my code. Thanks!


C++
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class record
{
public:
	int studentmarks[3];
	string studentname[3];
	int marks[3];
	
	void studentinfo()
	{
		ifstream input;
	input.open("save.txt");
	ofstream output;
	output.open("show.xls");

	string line;

	if(input.is_open())
	{
		while (getline(input,line))
		{
          
			output<<line<<endl;

		}


    }



	}


	void info()
	{
		ofstream myfile;
		myfile.open("save.txt");

		for(int i= 0; i<3; i++)
		{
         cout<<"Enter the name of students"<<endl;
		 getline(cin,studentname[i]);

        }

	for(int i= 0; i<3; i++)
		{
         cout<<"Enter the marks of students"<<endl;
		 cin>>studentmarks[i];

        }


	myfile<<"Name"<<"\t"<<"Marks"<<endl;
	for(int i =0; i<3; i++)
	{

		myfile<<studentname[i]<<"\t"<<studentmarks[i]<<endl;


	} 













	}


private:

};

int main()
{
	

	

	record c;
	c.info();

	c.studentinfo();

	
	system("pause");









}
Posted

1 solution

I assume that you wanted to say: Every time I call record::info the contents of the file save.txt is overwritten.

That is because you opened it without specifying a second argument in the open call. In this case the previous contents of the file is erased and overwritten. If you use std::ofstream::app as second argument, the new data will be appended at the end of the file.

Example:
C++
ofstream myfile;
myfile.open("save.txt", std::ofstream::app);
 
Share this answer
 
Comments
Aescleal 12-Nov-14 8:20am    
My 5...

As a completely separate aside: I've noticed that when questioners use fstreams they seem to open them separately from constructing them. Maybe it's me but that sounds like it's completely barmy. Maybe there's a tradition of crap C++ teaching in India which encourages this.
nv3 12-Nov-14 9:51am    
Thank you.
Surajit Das 12-Nov-14 10:49am    
@Aescleal:- Firstly, "crap C++ teaching in India" really? That's the only reasonable solution you have. I dont want to start a war here but this kind of comments shows your class. You might have a lot of knowledge but in my eyes you lost all respect. One more thing- When it comes to technology, Asians dominate it in every form.To name a few- Microsoft CEO-Satya Nadella ,Sundar Pichai(Google) and many more. May be we dominate the world because of our "so-called crap C++ teaching that we receive in India/Asia".Also watch out in the coming years- "Asians will dominate any sort of technology and jobs anywhere in the world" and we are thankful to the kind of education we receive in Asia/India. Peace
Aescleal 12-Nov-14 11:32am    
Don't be so sensitive - it looks like you've got a chip on your shoulder. I've just noticed that loads of Indian questioners use the form:

fstream str;
str.open( ... );

rather than the idiomatic:

fstream str( filename );

It doesn't seem as prevalent in European or US students but that might be because (a) the language is rarely taught outside of games programming degrees in Europe and the US and (b) less European and US students ask for help on here.

So a free bit of advice: Use the constructor form whenever you want to open a stream but be careful of the second, defaulted parameter - it often doesn't do what you want. If you do it'll make your code a lot easier to read in the long run and won't look so naive.
Surajit Das 12-Nov-14 10:51am    
@nv3:- Thank you for your help. I really appreciate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900