Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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]
# 1 Re: text
anyone?
tralfas at 2007-11-11 21:01:55 >
# 2 Re: text
Look at the following article. It explains how to use the <fstream> library. http://www.dev-archive.com/dev-archive/LegacyLink/9397 I don't know if your teacher used this library, but it should work.
Danny at 2007-11-11 21:03:00 >
# 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;
}
tralfas at 2007-11-11 21:03:59 >
# 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 >
# 5 Re: text
it still stops after a certain amount of characters can some one post a way to make it so that you can enter more words/characters :confused: ? should i add more strings to contain text or is there another way.
tralfas at 2007-11-11 21:06:05 >
# 6 Re: text
wait i answered my own question. just so i can better my knowledge what does this code do: .c_str()
tralfas at 2007-11-11 21:07:04 >
# 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 >