Thread: How to save?

  1. #1
    Inez
    Guest

    How to save?

    yea im still in console proggin...and i have yet another question...is it possible to save? like the user be able to completely exit the prog...and reneter and start where they left off? if so...could someone either explain how or tell me what to 'search' for?

  2. #2
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    perhaps using files. just as a generalized example you could save the entire state of your program by outputting all values of your variables into some output file. then when the user wants to return from the save, re-init all variables with the stored values. this is just a simple idea and i'm sure some more things would be involved.

  3. #3
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    one more thing, i remember some file accessing functions with standard 'C' library that enables you to output data to a file as a bit stream (rather than the standard sizes of built in types like int, char etc..), it made storing larger structures easier. you could also look up/search for "serializable objects" for other ways.

  4. #4
    Inez
    Guest
    How to input information to another file? and how to recall stuffs from that file!? all in console(or is this possible in console?

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If you saved the data into a file and then read that info from the file into the other program, then it would definately be possible. You will need to learn streams for this, specifically <fstream> and all its functions.

  6. #6
    Unregistered
    Guest
    Originally posted by Inez
    How to input information to another file? and how to recall stuffs from that file!? all in console(or is this possible in console?
    for starters here is some very simple file input output (note that it's not much different then inputting/outputting to the console, this is not a coincidence..)

    file output:

    Code:
    #include <iostream>
    #include <fstream>  //required for ofstream class
    
    int main()
    {
        ofstream output("test.txt");  //ofstream objects output to files,
                                                      //pass name of file to constructor
    
        output << 5;  //output the integer 5 to file
    
        output.close();  //close the file
        return 0;
    }

    file input:

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
        ifstream input("test.txt");  //ifstream objects read from file,
                                                  //pass existing filename to
                                                  //constructor
        int x;
        
        if(!input) {  //can test if file does not exist, or other reasons..
    	cerr << "file error!" << endl;
        }
    
        input >> x;  //read that integer we outputted earlier
        cout << x;  //output the integer to console to see if worked
    
        input.close();  //close the file
        return 0;
    }
    compile the code above. the first code snippet outputs an integer to a file called "test.txt", the second file reads that integer from "test.txt" and outputs to the console. i used the .txt extension (assuming you use windows) so that you can open the file up yourself and see that the integer is in there. run the program in a new directory (doing this makes things easier when dealing with file i/o).

    also, you can output any built-in c++ data type you want (int, char, float ...); and as many as you want. problem is, you will have to know how many and of what type to read back from the file so you will have to organize this yourself. example:

    int x = 5;
    char c = 'a';
    double z = 5.5;

    output << x << c << z;

    //then you have to input in the correct order:

    input >> x >> c >> a;

    and there are other things like what "EOF" is, can use it to read an unknown number of items from a file (kind of lets you know your file pointer has reached the end of file).

    point is, you should really check out a good C++ book that covers this stuff in more detail; well worth it.

  7. #7
    Inez
    Guest
    wow thanks alot...yea i need to go get a book here soon...havent been proggin for long...

    just one more question tho...when i output several numbers when i input them they all get cluttered up...

    exp:
    #include <fstream> //required for ofstream class

    void output()
    {
    ofstream output("test.cpp"); //ofstream objects output to files,
    //pass name of file to constructor

    output <<3<<4<<5; //output the integer 5 to file

    output.close(); //close the file
    }


    void input()
    {
    ifstream input("test.cpp"); //ifstream objects read from file,
    //pass existing filename to
    //constructor


    if(!input) { //can test if file does not exist, or other reasons..
    cerr << "file error!" << endl;
    }

    input >> x>>y>>z; //read that integer we outputted earlier
    cout << x<<","<<y<<","<<z; //output the integer to console to see if worked

    input.close(); //close the file
    }


    result: 345,0,0


    how to let the comp know to seperate the numbers?

  8. #8
    Inez
    Guest
    uhm test.txt not test.cpp(i was trying something earlier forgot to change~)

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >how to let the comp know to seperate the numbers?

    You read/write to the file in binary mode, otherwise the int's/whatever will just get converted to and from their ASCII equivalents which will 'roll' into each other and wont be based on the size of the variable type that you are reading and writing. Otherwise you'll have to write them to the file in text mode with a seperator (space, tab,dash, etc) and then take this into account when reading the file.

  10. #10
    Inez
    Guest
    >You read/write to the file in binary mode

    what is binary mode? (yea im uber newbit)

    is it that deal where you use the pointer to find a variables address and it gives you that string of numbers/letters?

  11. #11
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    Originally posted by Inez
    >You read/write to the file in binary mode

    what is binary mode? (yea im uber newbit)

    is it that deal where you use the pointer to find a variables address and it gives you that string of numbers/letters?
    as Sorenson says, you can separate these with spaces.


    //when outputting:

    output << 3 << ' ' << 4 << ' ' << 5;

    //then inputting:

    input >> x >> y >> z;

    //then you will have: x = 3, y = 4, z = 5

    (don't worry about binary mode for something simple with ints)

  12. #12
    Inez
    Guest
    int *pointer;

    void output()
    {
    int num(5);

    pointer=&num;
    ofstream output("test.txt"); //ofstream objects output to files,
    //pass name of file to constructor

    output <<pointer; //output the integer 5 to file
    cout <<pointer<<endl<<num<<endl;
    output.close(); //close the file
    }


    void input()
    {
    ifstream input("test.txt"); //ifstream objects read from file,
    //pass existing filename to

    if(!input) { //can test if file does not exist, or other reasons..
    cerr << "file error!" << endl;
    }
    int *poin;
    int y;
    poin=&y;
    input >>*poin; //read that integer we outputted earlier

    cout <<y-1; //output the integer to console to see if worked

    input.close(); //close the file
    }


    int main()
    {
    output();
    input();
    system("pause");
    return 0;

    }

    is that what i need to do?

    also question...when i input the info i had to -1 from y(to get 5)...why is this?

  13. #13
    Inez
    Guest
    Ah yea...well i figured out the whole just put a space inbetween them...jus another question...how can you pick and choose what you want to be 'inputted'?

  14. #14
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    Originally posted by Inez
    Ah yea...well i figured out the whole just put a space inbetween them...jus another question...how can you pick and choose what you want to be 'inputted'?
    a file pointer. but now things are just getting a little bit more advanced. the idea of the file pointer is that you can move this pointer throught a file, but to do this you must be able to know how far to move the pointer within the file to get the value you want. i've never messed around with the file pointer, and actually know nothing about file pointers with C++ (only C); maybe it's easier with C++. but if you have several different "types" in the file, you will have to know how many sizeof(types) to move about the file to get the object/value you want. i think... what kind of program are you working on anyway? maybe there's an easier way to do things than to start messing with file pointers.

  15. #15
    Inez
    Guest
    hm...file pointers...yea heh...all this stuff is getting jus a little complex...ill haveta jimmy rig a k00 way to make this work...
    right now im just making little programs to extend my knowledge of c++...i jus think up what i want...and when i encounter a problem i try to solve it...seems to be a good way to teach c++ to myself...

    heh...c++ is my first proggin language...i havent even done any html....*shrugs*

    thanks everyone for your help...ill be messing with that binary stuffs...and file pointers...and everything else...many more questions later...but for now...thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 02:42 PM
  2. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  3. save and save as functions
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 06-29-2005, 02:49 AM
  4. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM