Saving File

This is a discussion on Saving File within the C++ Programming forums, part of the General Programming Boards category; for some reason when i retieve the file back it leaves out name, last name and age then comes out ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Saving File

    for some reason when i retieve the file back it leaves out name, last name and age then comes out with a load of numbers

    can anyone help?

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    void other()
    {
        char FirstName[30], LastName[30];
        int Age;
        char FileName[20];
        cout << "Enter the name of the file you want to open: ";
        cin >> FileName;
        ifstream Students(FileName);
        Students >> FirstName >> LastName >> Age;
    
    	cout << "\nFirst Name: " << FirstName;
        cout << "\nLast Name:  " << LastName;
        cout << "\nEnter Age:  " << Age;
    
        cout << "\n\n";
        system("pause");
    }
    
    int main()
    {
    
        char FirstName[30], LastName[30];
        int Age;
        char FileName[20];
        
        cout << "Enter First Name: ";
        cin >> FirstName;
        cout << "Enter Last Name:  ";
        cin >> LastName;
        cout << "Enter Age:        ";
        cin >> Age;
    
        cout << "\nEnter the name of the file you want to create: ";
        cin >> FileName;
        ofstream Students(FileName, ios::out);
        Students << FirstName << "\n" << LastName << "\n" << Age;
        system ("pause");
        other();
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Don't forget to close streams!! Students.close() etc. Otherwise the files may not be accessable.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    where abouts will that need to go?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ok, think about it this way - whenever you're finished with writing to or reading from a file, CLOSE IT!

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    got it mate, thanx

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> ofstream Students(FileName, ios :: out);

    it's an ofstream, so I don't think you need the ios :: out ... I may be wrong of course.

    ofstream out ( "thing.txt" );

    normally works.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by twomers
    Don't forget to close streams!! Students.close() etc. Otherwise the files may not be accessable.
    You should check that the file is opened (for example, using Students.good() ). In this case, however, closing the file is not necessarily the problem. Try adding a std::flush after outputting all your data. It should have the same effect as closing the file (which will flush the stream first)

    Edit: here's what I mean:
    Code:
    Students << FirstName << "\n" << LastName << "\n" << Age << std::flush;
    system ("pause");
    other();
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    just a suggestion why dont you use C++ strings, instead of C strings? isnt
    Code:
    string name = "";
    cout << "Choose a name: " << endl;
    getline( cin, name, '\n' );
    a little more C++ish than
    Code:
    char name[20];
    cout << "Choose a name: " << endl;
    getline( cin, name, '\n' );
    I dont maybe it is just me but it seems a little better to use c++ strings
    also one more thing you should do something like this on your ifstream's
    Code:
    ifstream in ( "somefile.ext" );
    if ( !in.is_open() ) {
        cout << "File does not exist." << endl;
    }
    else {
        // file opened successfully
    }
    in.close();
    just a couple ideas enjoy

    [EDIT]
    umm, you need to use
    Code:
    getline ( in, name, '\n' );
    in order to get the spaces in the name if you use cin >> var; then it only goes to the first whitespace.
    Last edited by Raigne; 09-04-2006 at 07:08 PM.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Raigne: If is_open() fails, the file may still exist, but may not be able to be opened for reading; if another process has locked the file, for example.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Well, what I meant was it does not exist to the program at that time

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    Lightbulb

    try replacing the 'Other' function with this one.


    void other()
    {
    char FirstName[30], LastName[30],ch;
    int Age;
    char FileName[20];
    cout << "Enter the name of the file you want to open: ";
    cin >> FileName;
    ifstream Students(FileName);
    Students >> FirstName >>ch>> LastName >>ch>> Age;

    cout << "\nFirst Name: " << FirstName;
    cout << "\nLast Name: " << LastName;
    cout << "\nEnter Age: " << Age;

    cout << "\n\n";
    system("pause");
    }

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    @achy1729 , please use the code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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