pls help me

hi guys, so i am using this code to write to and read from a txt. file but when i write it shows me only the first word while the rest gone. when i write connected words, without space, it shows the same thing i wrote, pls refer to the code below

// writing on a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void write_txt(char complain[512])
{
ofstream outfile;
outfile.open("Hamzahtrial.txt",ios::trunc |ios::in | ios::out
); //for updating the status
cin>>complain;
cin.ignore();
outfile<<complain<<endl;
outfile.close();
}
void read_txt(string line)
{

ifstream myfile ("hamzahtrial.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";
}
void call (long recall)
{
int g;
string line;
char complain[512];
cout<<"pls press 0 or 1";
cin>>g;
if (g == 0)
{
write_txt(complain);

}
// reading a text file
else if (g == 1)
{
read_txt(line);
}
else
cout<<":";
}
int main ()
{
long recall;
call(recall);

system("pause");
return 0;
}
getline(cin, g);
what is g???
i tried but it gives an error
make complain a string and use it like you(?) did here: getline (myfile,line);

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
ok i got it, it works only when i put it in the main ,int main().
but it doesnt work when i put in a function
do u know why???
i am talking about ur first comment
> when i write it shows me only the first word while the rest gone.

Open the file with std::ios::app to append to a file
1
2
3
4
// ofstream outfile;
// outfile.open("Hamzahtrial.txt",ios::trunc |ios::in	| ios::out
// ); //for updating the status 
ofstream outfile( "Hamzahtrial.txt", ios::app ) ; // append to file 
it still not working when using function.
it works in the main.
i am supposed to put in a function
ok i got it, it works only when i put it in the main ,int main().
but it doesnt work when i put in a function
What do you put in main()? and what does(n't) work?
thank u all
this is what i am looking for
i got it from cplusplus site
// typewriter
#include <iostream>
#include <fstream>
using namespace std;

int main () {

char ch;
ofstream outfile ("test.txt");

do {
ch=cin.get();
outfile.put (ch);
} while (ch!='.');

return 0;
}
Topic archived. No new replies allowed.