Save to File

This is a discussion on Save to File within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me what's wrong with this ? Thanx... I'm just trying to get a save-game function, but I ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Save to File

    Can anyone tell me what's wrong with this ? Thanx... I'm just trying to get a save-game function, but I can't get it to work...



    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    char name[50];
    int age[50];
    char location[50];
    char birthplace[50];
    char filename[50];
    char xit;
    
    
    ifstream infile(filename);
    ofstream outfile(filename);
    
    int main()
    
    {
    
    
    cout << "Name ?" << endl << endl;
    cin >> name[50];
    
    cout << "\n\n\nAge ?" << endl << endl;
    cin >> age[50];
    
    cout << "\n\n\nLocation ?\n\n";
    cin >> location[50];
    
    cout << "\n\n\nBirthplace ?\n\n";
    cin >> birthplace[50];
    
    cout << "\n\n\nSave info where ?\n\n";
    cin >> filename[50];
    
    infile << name[50];
    infile << "\n";
    infile << age[50];
    infile << "\n";
    infile << location[50];
    infile << "\n";
    infile << birthplace[50];
    
    cin >> xit;
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i believe "infile <<" should be "infile>> "

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Talking

    OOPS !

    This is why I hate computers sometimes... too stupid to understand human error... maybe it's just me

    And NOW I get it running, I type in name, and I get a fatal error...
    Anyone ( again ) ? Thanx a lot for your support guys...

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    cout << "Name ?" << endl << endl;
    cin >> name[50];
    
    cout << "\n\n\nAge ?" << endl << endl;
    cin >> age[50];
    
    cout << "\n\n\nLocation ?\n\n";
    cin >> location[50];
    
    cout << "\n\n\nBirthplace ?\n\n";
    cin >> birthplace[50];
    
    cout << "\n\n\nSave info where ?\n\n";
    cin >> filename[50];
    
    infile << name[50];
    infile << "\n";
    infile << age[50];
    infile << "\n";
    infile << location[50];
    infile << "\n";
    infile << birthplace[50];
    Remove the "[50]" off of everything there. (but leave it on your variable declarations) You need to read in an entire string, but by passing the 50th element to the stream, you're actually reading into the memory occupied by the null terminator... and any memory after that.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    cin >> name[50];

    Get rid of the brackets. eg:

    cin >> name;
    infile >> name;
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    24 C:\My Documents\delete\Untitled1.cpp
    initializing non-const `bool &' with `int *' will use a temporary

    217 C:\PROGRAMMING\DEV-CPP\include\g++-3\iostream.h
    in passing argument 1 of `istream:perator >>(bool &)'


    Hum... Us newbies need help... thanx for living up to it !

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    okay, change int age[50] to char age[50], it will compile

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Sorry guys, still not. It compiles, the thing runs fine until it has to create the file, where I get another fatal error.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Exactly.

    int age[50]; /* an array of 50 birthdays! */

    char age[50]; /* reserve 50 chars to store a number in character format */
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Post the entire program then.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  11. #11
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    #include <iostream.h>
    #include <fstream.h>

    char name[50];
    char age[50];
    char location[50];
    char birthplace[50];
    char filename[50];
    char xit;


    ifstream infile(filename);
    ofstream outfile(filename);

    int main()

    {


    cout << "Name ?" << endl << endl;
    cin >> name;

    cout << "\n\n\nAge ?" << endl << endl;
    cin >> age;

    cout << "\n\n\nLocation ?\n\n";
    cin >> location;

    cout << "\n\n\nBirthplace ?\n\n";
    cin >> birthplace;

    cout << "\n\n\nSave info where ?\n\n";
    cin >> filename;

    infile >> name;
    infile >> "\n";
    infile >> age;
    infile >> "\n";
    infile >> location;
    infile >> "\n";
    infile >> birthplace;

    cin >> xit;

    return 0;

    }

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Save to File

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    char name[50];
    int age;
    char location[50];
    char birthplace[50];
    char filename[50];
    char xit;
    
    ofstream outfile;
    
    int main()
    {
    
    cout << "Name ?" << endl << endl;
    cin >> name;
    
    cout << "\n\n\nAge ?" << endl << endl;
    cin >> age;
    
    cout << "\n\n\nLocation ?\n\n";
    cin >> location;
    
    cout << "\n\n\nBirthplace ?\n\n";
    cin >> birthplace;
    
    cout << "\n\n\nSave info where ?\n\n";
    cin >> filename;
    
    outfile.open(filename);
    
    outfile<< name;
    outfile<< '\n';
    outfile<< age;
    outfile<< '\n';
    outfile<< location;
    outfile<< '\n';
    outfile<< birthplace;
    
    cin >> xit;
    
    return 0;
    
    }
    You'll probably want to use the outfile for saving data.
    Also, you open your files by passing the uninitilized "filename" variable, then later you ask which file you'd like to save to. You should place the initilization (opening) of the outfile between your calls to cin and cout and file manipulation. Above is the revised code.
    Last edited by Eibro; 12-01-2002 at 12:18 PM.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    char name[50];
    char age[50];
    char location[50];
    char birthplace[50];
    char filename[50];
    char xit;
    
    
    int main()
    {
    
    cout << "Name ?" << endl << endl;
    cin >> name;
    
    cout << "\n\n\nAge ?" << endl << endl;
    cin >> age;
    
    cout << "\n\n\nLocation ?\n\n";
    cin >> location;
    
    cout << "\n\n\nBirthplace ?\n\n";
    cin >> birthplace;
    
    cout << "\n\n\nSave info where ?\n\n";
    cin >> filename;
    
    ofstream outfile(filename);
    
    outfile >> name;
    outfile >> "\n";
    outfile  >> age;
    outfile  >> "\n";
    outfile  >> location;
    outfile  >> "\n";
    outfile  >> birthplace;
    
    cin >> xit;
    
    return 0;
    
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  14. #14
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Guys, sorry, still not working, either of them...

    While I'm here, could I ask how to declare a char with unlimited length ?

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    outfile >> name;
    outfile >> "\n";
    outfile  >> age;
    outfile  >> "\n";
    outfile  >> location;
    outfile  >> "\n";
    outfile  >> birthplace;
    Wrong operator. Use << for ofstream, not >>.
    There's no such thing as a char with unlimited length... nor is there a practical use for one. Oh, and try the code from my last post, I had copied the wrong code... it's fixed now.
    Last edited by Eibro; 12-01-2002 at 12:22 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save a file with a name in a variable?
    By guriwashere in forum C Programming
    Replies: 2
    Last Post: 06-01-2009, 04:03 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 05:34 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21