i ask the user to input something and the result is stored.what should i do to save whatever is there on the runtime screen to the hard disk?
i ask the user to input something and the result is stored.what should i do to save whatever is there on the runtime screen to the hard disk?
Yes, you can save data to a file. Look up "File IO" in the tutorial that is on the main site here.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Something like this will save an entered number into a text file named file in the directory of the cpp file
Code:#include <fstream> //include file stream #include <iostream> int main() { ofstream file("file.txt"); //opens file output stream int enteredVal; cout << "Enter A Value"; cin >> enteredVal; file << enteredVal; // writes object to file file.close(); // closes file stream return 0; }
thanks a lot.....
i ran the program it runs great.
but the re run it again the the old value gets erased!.
what should i do?
rgds
not that experienced with c++ so i recommend you look up fstream but if you want a cheap way of doing it try reading and saving the file content first so something like.
this code is definitly not the best way to do it. You need to read up on file streams and fabricate code to suit your needs specifically.Code:#include <fstream> //include file stream #include <iostream> int main() { int readVal; ifstream fileRead("file.txt") //opens file input stream fileRead >> readVal; fileRead.close() ofstream file("file.txt"); //opens file output stream int enteredVal; cout << "Enter A Value"; cin >> enteredVal; file << readVal << enteredVal; // writes objects to file file.close(); // closes file stream return 0; }
At this point, it would probably make sense if you (Saimadhav) explained in more detail overview terms what your program is doing. Saving some data and reloading it next time around is very generic as a concept. You have been given solutions for that, but apparently not quite right. So then we need to know more about what you are trying to do. Perhaps you want some sort of log/journal of what has been done (bank transactions would be a typical case of this), or most recently used list (for example your web-history), or a list of some other sort (say a top-ten list for users of a game, perhaps?)
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
First of all, how do you want to store the information in the file? You have to decide this first. Do you want each time to save the value next to the previous value? Do you want the values to be separated by a space? Do you want one value in each line of the file? Think the file as a text.
Second, you have to determine the position of which you want the information to be stored inside the file. When you open the file you are at the beginning of the file. If you write something there, close the file, open it again you are at the beginning again. So you will write the value in the same position. A way to solve this is to start from the end of the file (I think readFile.seekg(0, ios_base::end) at the above example). And don't forget to separate the values somehow by adding a character (like space) in between the read/write
Check this http://www.cplusplus.com/doc/tutorial/files.html
thanks matsp for your concern. i am 15 . i am a beginner.i was thinking about a program that would act as a report card . so the marks of each student gets stored in a txt file. i then move it to a html document because it gets aligned and i then give the print command.and the job is done.
thanks