-
October 15th, 2012, 03:28 PM
#1
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
-
October 16th, 2012, 09:47 AM
#2
Re: Save and Read struct help? binary file
Originally Posted by s123456
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
- 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! - If you "want to display all four records" then you have put the read statement inside the loop.
Victor Nijegorodov
-
October 16th, 2012, 03:23 PM
#3
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 . . .
-
October 16th, 2012, 03:33 PM
#4
Re: Save and Read struct help? binary file
Originally Posted by s123456
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;
}
- What this highlighted "statement" could mean?
- Please, use Code tags while posting code snippets!
Victor Nijegorodov
-
October 16th, 2012, 04:58 PM
#5
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
-
October 17th, 2012, 03:01 AM
#6
Re: Save and Read struct help? binary file
Originally Posted by s123456
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|