| | |
Reading out to a file/Null String Troubles
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Solved Threads: 0
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.
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.
cpp Syntax (Toggle Plain Text)
else if (decision == "S" || decision == "s"){ cout << "File name? " <<"("<<filename<<")"<< endl; cin >> newfile; if (newfile == '/0'){ outdata.open("filename"); for (k =0; k < rows; k++){ for (m =0; m < cols; m++) outdata<< (image[k][m]); outdata<< "\n"; outdata.close(); } } return 0; // else { // outdata.open("newfile"); // for (k =0; k < rows; k++){ // for (m =0; m < cols; m++) // outdata<< (image[k][m]); // outdata<< "\n"; // outdata.close(); // } // } // return 0; }
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...
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:
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:
A very useful website is http://www.cppreference.com/
Hope this helps.
[code]
std::cout << "Hello, world!" << std::endl;
[/code]
which looks like this:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
std::getline( std::cin, filename ); if (filename.empty()) filename = "newfile.txt"; outdata.open( filename.c_str() );
Hope this helps.
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
else if (decision == "S" || decision == "s"){ cout << "File name? " <<"("<<filename<<")"<< endl; cin >> newfile; if (newfile.empty()) { outdata.open(filename.c_str() ); for (k =0; k < rows; k++){ for (m =0; m < cols; m++) outdata<< (image[k][m]); outdata<< "\n"; } outdata.close(); } else { outdata.open(newfile.c_str() ); for (k =0; k < rows; k++){ for (m =0; m < cols; m++) outdata << (image[k][m]); outdata << "\n"; } outdata.close(); } return 0; }
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
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.
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.
![]() |
Similar Threads
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- Reading a file line by line problem (C)
- First year assigment on reading file, sorting and outputting invoice (C++)
- broken code (C++)
- Perl Whitespaces (Perl)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Newbie needs help....please!
- Next Thread: Non-recursive quicksort help
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple spoonfeeding string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






