Reading out to a file/Null String Troubles

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Reading out to a file/Null String Troubles

 
0
  #1
Oct 18th, 2007
This is my first time working with outputting data to a file, so I've got some rough code that I need some pointers on.
  1. else if (decision == "S" || decision == "s"){
  2. cout << "File name? " <<"("<<filename<<")"<< endl;
  3. cin >> newfile;
  4. if (newfile == '/0'){
  5. outdata.open("filename");
  6. for (k =0; k < rows; k++){
  7. for (m =0; m < cols; m++)
  8. outdata<< (image[k][m]);
  9. outdata<< "\n";
  10. outdata.close();
  11. }
  12. }
  13. return 0;
  14. // else {
  15. // outdata.open("newfile");
  16. // for (k =0; k < rows; k++){
  17. // for (m =0; m < cols; m++)
  18. // outdata<< (image[k][m]);
  19. // outdata<< "\n";
  20. // outdata.close();
  21. // }
  22. // }
  23. // return 0;
  24. }
If the person doesn't enter anything then it overwrites the file that was opened, and if the user enters something then it just pours the image into that filename. I'm not sure how to use null string, and also the outdata.open requires quotation marks around whatever is inside the parenthesis so I can't use the two strings filename and newfile. And I turned my else statement into a comment at the moment because the compiler doesn't like it so I'm trying to figure out what's wrong with that now.
Last edited by WaltP; Oct 18th, 2007 at 5:06 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading out to a file/Null String Troubles

 
0
  #2
Oct 18th, 2007
Er, when you post code make sure to place it between 'code' blocks, like this:

[code]
std::cout << "Hello, world!" << std::endl;
[/code]

which looks like this:
  1. std::cout << "Hello, world!" << std::endl;

I assume that filename is a std::string and outdata is a std::ostream. The stream classes are meant for parsing data, not pure string input. Instead, say:
  1. std::getline( std::cin, filename );
  2. if (filename.empty()) filename = "newfile.txt";
  3.  
  4. outdata.open( filename.c_str() );
A very useful website is http://www.cppreference.com/

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Re: Reading out to a file/Null String Troubles

 
0
  #3
Oct 18th, 2007
Thanks for the help, I am now able to save to a file if the user enters one, however it still doesn't work if the user doesn't enter a filename. If that happens, the program is to assume the user wants to overwrite the file that they originally opened.

  1. else if (decision == "S" || decision == "s"){
  2. cout << "File name? " <<"("<<filename<<")"<< endl;
  3. cin >> newfile;
  4. if (newfile.empty()) {
  5. outdata.open(filename.c_str() );
  6. for (k =0; k < rows; k++){
  7. for (m =0; m < cols; m++)
  8. outdata<< (image[k][m]);
  9. outdata<< "\n";
  10. }
  11. outdata.close();
  12. }
  13. else {
  14. outdata.open(newfile.c_str() );
  15. for (k =0; k < rows; k++){
  16. for (m =0; m < cols; m++)
  17. outdata << (image[k][m]);
  18. outdata << "\n";
  19. }
  20. outdata.close();
  21. }
  22. return 0;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading out to a file/Null String Troubles

 
0
  #4
Oct 18th, 2007
It isn't working because you have not obeyed my advice. The user can hit enter a zillion times and it won't make any difference as long as you are using cin >> newfile;.

Also, notice how most of the lines you have written are exactly the same. Put all the stuff that writes to file by itself, once, after the file is opened using your if...else statement.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC