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