CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    8

    Save and Read struct help? binary file

    here is my code:

    /////////////////////////////////////////////struct

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    struct Cat
    {
    char name[20];
    int age;


    };
    //////////////////////////////////////////////////////////////



    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    #include "Cat.h";



    int main()
    {
    Cat yourCat;
    int NumStructs;

    int num = 3;
    int count = 0;

    fstream file;

    cout << "Enter 3 cat records.\n";

    file.open("f:\\critters.bin", ios:ut | ios::binary);

    for(int i = 0; i < 3; i++)
    {
    cout << "Enter information about a cat:\n";
    cout << "NAME: ";
    cin >> yourCat.name;
    cout << "AGE: ";
    cin >> yourCat.age;
    file.write(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    }

    cout << "Record written to file.\n";
    file.close();

    cout << "\n\nEnter one more cat\n";
    file.open("f:\\critters.bin", ios::app);
    if (file.fail()) {
    cerr << "Unable to open file for writing." << endl;
    exit(1);
    }

    cout << "NAME: ";
    cin >> yourCat.name;
    cout << "AGE: ";
    cin >> yourCat.age;
    file.write(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    file.close();

    cout << "\n\nHere is a list of all cats:\n";



    file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    for(int i = 0; i< 4;i++)
    cout << setw(10) << left << yourCat.name <<endl;
    cout << yourCat.age <<endl;
    file.close();



    system("pause");
    return 0;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////
    //////////////////////////////////
    .



    I NEED HELP WITH THIS:


    file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    for(int i = 0; i< 4;i++)
    cout << setw(10) << left << yourCat.name <<endl;
    cout << yourCat.age <<endl;
    file.close();





    //// i want to display all four records ...example:
    Here is a list of all cats:
    Tom 5
    Fluffy 3
    Sally 4
    Sam 2

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Save and Read struct help? binary file

    Quote Originally Posted by s123456 View Post
    I NEED HELP WITH THIS:

    file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    for(int i = 0; i< 4;i++)
    cout << setw(10) << left << yourCat.name <<endl;
    cout << yourCat.age <<endl;
    file.close();


    //// i want to display all four records ...example:
    Here is a list of all cats:
    Tom 5
    Fluffy 3
    Sally 4
    Sam 2
    1. Your for loop consists only of one statement
      Code:
      for(int i = 0; i< 4;i++)
        cout << setw(10) << left << yourCat.name <<endl;
      It is because you do not use the braces!
    2. If you "want to display all four records" then you have put the read statement inside the loop.
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2012
    Posts
    8

    Re: Save and Read struct help? binary file

    like this?


    for(int i = 0; i< 4;i++)
    { cout << file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    cout << setw(10) << left << yourCat.name << "\t";
    cout << yourCat.age <<endl;
    }

    file.close();

    //===========================OUTPUT==============================
    Enter 3 cat records.
    Enter information about a cat:
    NAME: a
    AGE: 1
    Enter information about a cat:
    NAME: b
    AGE: 2
    Enter information about a cat:
    NAME: c
    AGE: 3
    Record written to file.


    Enter one more cat
    NAME: d
    AGE: 4


    Here is a list of all cats:
    00000000d 4
    00000000d 4
    00000000d 4
    00000000d 4
    Press any key to continue . . .

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Save and Read struct help? binary file

    Quote Originally Posted by s123456 View Post
    like this?
    Code:
    	for(int i = 0; i< 4;i++)
    		{	cout << file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    			cout << setw(10) << left << yourCat.name << "\t";
    			cout << yourCat.age <<endl;
    		}
    1. What this highlighted "statement" could mean?
    2. Please, use Code tags while posting code snippets!
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2012
    Posts
    8

    Re: Save and Read struct help? binary file

    You said to put the read statement inside the loop ...it didn't work...so I need help making it work

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Save and Read struct help? binary file

    Quote Originally Posted by s123456 View Post
    You said to put the read statement inside the loop ...it didn't work...so I need help making it work
    But I never said that you should pass in the return value of read(...) to cout!
    Code:
    	for(int i = 0; i< 4;i++)
    	{	
    		file.read(reinterpret_cast<char*>(&yourCat), sizeof(struct Cat));
    		cout << setw(10) << left << yourCat.name << "\t";
    		cout << yourCat.age <<endl;
    	}
    Besides, you must open this file for reading before trying to read anything!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured