text
well i have seen the other program that is for that writes text to a txt document. but i didnt understand exactly how he didnt. i messed around with the code but didnt get it. can some one show me really quick.
i just want the user to be able to enter text, whatever text the user wants and then the program makes a file containing that text.
[351 byte] By [
tralfas] at [2007-11-11 7:45:38]

# 3 Re: text
this program allows the user to name the file and then write text to a .txt file. the only problem is that the program limits how many characters the user can enter. how can i fix this?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
ofstream myfile;
char text[300];
char text2[20];
cout << "name your file (example.txt)" << endl;
cin.getline(text2,20);
cout << "enter your text" << endl;
cin.getline(text,300);
myfile.open (text2);
myfile << text;
myfile.close();
system ("pause");
return 0;
}
# 4 Re: text
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
ofstream file;
string fileName;
string text;
cout << "Enter the name of the file: ";
cin >> fileName;
cout << "Enter the text to write to " << fileName << ": ";
cin >> text;
file.open(fileName.c_str());
file << text;
file.close();
return 0;
}
destin at 2007-11-11 21:04:59 >

# 7 Re: text
c_str() returns a const char * representation of the string object. You need to use it when a function call expects a const char * argument, and you're using a std::string object. A classic example is opening a file whose name is stored in a string object. fstream::open accepts onlt char/wchar_t * names, not strings.
Danny at 2007-11-11 21:08:08 >
