Thread: saving

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    saving

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    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;
    }

  4. #4
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    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

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    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.

    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;
    }
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Saimadhav View Post
    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
    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

  8. #8
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ problem saving files
    By wesm in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 02:00 PM
  2. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  3. saving a binary tree??
    By fayte in forum C++ Programming
    Replies: 9
    Last Post: 01-27-2005, 01:32 PM
  4. Saving vectors of structures problem
    By redeck in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 04:47 PM
  5. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 01:17 AM