Hey guys , im having a problem writing into a file. Basically ,i have a program which read an input from one file , and then calls it to the function of another method. Here is how my program looks like.

using namespace std;


    class Team{
    public :
        void Readfile() ;
    };

    class Player:public Team
    {
    public:
        void TransferInfo(string) ;


    };



void Team::Readfile(){
    Player pl;
    string line;
    fstream  reader("test.txt");
      while (getline(reader ,line))
    {
          pl.TransferInfo(line);
                    }
}

void Player::TransferInfo(string i ){
    ofstream myfile("test2.txt");
    
    string t  = i;
    string r = t + "new";

    myfile << r << endl;

   
}



int main (){

    Player ps;
    ps.Readfile() ;

}

My test.txt file contains data in this format...

John
Mellisa

What i am trying to achieve is , when the input is being read , it will be sent to another function which will tag a new string to the input. Thus , John will become "Johnnew" and Mellisa will be "Mellisanew".

The problem i face is writing it to a new file. It is only able to write in the last input of the file , in this case Mellisanew.

I guess it has to do with the problem of calling the function. Is there any way i can solve this?

My expected output in the file test2.txt is...

Johnnew
Mellisanew

Have you stepped through your code to make sure your TransferInfo method is being called for each person?

yeap. it is being called for each person. The problem seem to come from the ofstream. It just writes on a single line. so, whenever a new input comes it, it deletes the previous entry and writes over it.

Oh I see, I misread your initial post. You need to call the ofstream constructor with the "Append" file bitflag. Right now you're asking it to overwrite the file everytime you open it.

A quick google for ofstream.open will tell you all you need to know =)

If you want to open a file multiple times and add new information to it each time, you have to use append mode. If you don't (you only use output mode) you overwrite the existing file data every time.

Try changing Line 30 to ofstream myfile("test2.txt", ios::app); instead.

[edit]
oops.... overlapped w/ Kets...

Thanks dude , it worked. But it seems to put everything on the same line.

i tried using

myfile << r << "\n";

"\n" is suppose to make it continue to the next line. yet im not getting it. Any reason why? its displaying the info as "JohnnewMellisanew"

Use "endl" instead. The character recognized as the end-of-line character varies based on platform. The manipulator endl should help compensate for the variation.

myfile << r << endl;

you meant this right?

tried using it but its still writing everything on the same line.

Try outputting the newline first.

sorry , what did u mean by it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.