By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Text Files

compman9902
100+
P: 105
I know that this subject is a little drawn out, but I do need some help.
I have searched google for a while but still can't seem to get an answer.
All I need is the code for a few things (links to tutorials will do fine as well).
1.) Creating a text file at a user-defined location and then filling it with a few lines of pre-assigned numbers
2.)Reading a text file at a user-defined location and then creating a second text file using the original file name at the same location (ex. Reads ex.txt and makes Eex.txt)
That is all I need. Here is what I'm working with:
Expand|Select|Wrap|Line Numbers
  1. string locationOne;
  2. string locationTwo;
  3. string nameOne;
  4. string nameTwo;
  5. string filler;
  6. cout << "Please Enter The Location Of The File You Want To Write To: " << endl;
  7. cin   >> locationOne;
  8. cout << "Please Enter The Location Of The File You Want To Read: " << endl;
  9. cin  >> locationTwo;
  10. cout << Please Enter The Name Of The File You Want To Write To: << endl;
  11. cin  >> nameOne;
  12. nameTwo = "E" + nameOne;
  13.  
locationOne = The File To Write To
locationTwo = The File To Read
nameOne = The Name Of The File To Write To
name Two = The Name Of The Modified File To Write To
filler = What I put in the text files
Please either give me an example using the above variables or give me a link to a helpful tutorial
Thank You
Mar 21 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
have a look at
http://www.cplusplus.com/doc/tutorial/files.html
Mar 21 '07 #2

compman9902
100+
P: 105
have a look at
http://www.cplusplus.com/doc/tutorial/files.html
I have and I have to tell you..I don't really understand it.
Every time I try to do this:
Expand|Select|Wrap|Line Numbers
  1. ofstream myfile;
  2. myfile.open (locationOne);
  3. myfile << "Writing this to a file.\n";
  4. myfile.close();
  5.  return 0;
I get an error on the "myfile.open (locationOne);" part.
Mar 21 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
This is because your compiler expects a character array (cstring) in the open() command rather than a string. Instead of
Expand|Select|Wrap|Line Numbers
  1. myInputStream.open(myString);
write

Expand|Select|Wrap|Line Numbers
  1. myInputStream.open(myString.c_str());
and the error should be fixed.
Mar 21 '07 #4

compman9902
100+
P: 105
This is because your compiler expects a character array (cstring) in the open() command rather than a string. Instead of
Expand|Select|Wrap|Line Numbers
  1. myInputStream.open(myString);
write

Expand|Select|Wrap|Line Numbers
  1. myInputStream.open(myString.c_str());
and the error should be fixed.
That only answers a few of my questions. Also, please tell me how to open the file save and file open dialogs
Thanks
Mar 22 '07 #5

Roonie
P: 99
you can use myInputStream.getline() to read in lines from your file. you can then feed them into a new file that you create in much the same way as the first.
Mar 22 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.