Hi!
^.^
I am making a program but I can't continue because there's something wrong...
[code=cpp]#include<iostre am>
#include<string >
#include<fstrea m>
using namespace std;
struct record{
int IdentificationN umber;
string Name;
int Quantity;
double Cost;
};
main(){
record TheTool;
fstream file("hardware. dat", ios::in|ios::ou t|ios::binary);
if(!file){
cerr<<"File could not be opened or created"<<endl;
exit(1);
}
cout<<"I Will Add Something To The File"<<endl;
cout<<"Enter tool ID Number: ";
cin>>TheTool.Id entificationNum ber;
cout<<"Enter tool name: ";
cin.ignore();
getline(cin, TheTool.Name);
cout<<"Enter Quantity: ";
cin>>TheTool.Qu antity;
cout<<"Enter Cost: ";
cin>>TheTool.Co st;
file.seekp((The Tool.Identifica tionNumber-1)*sizeof(recor d));
file.write(rein terpret_cast<co nst char *> (&TheTool), sizeof(record)) ;
file.close();
fstream file1("hardware .dat", ios::in|ios::ou t|ios::binary);
file1.seekg(0);
file1.read(rein terpret_cast<ch ar*>(&TheTool), sizeof(record)) ;
while(file1.goo d()){
cout<<TheTool.I dentificationNu mber<<TheTool.N ame<<TheTool.Qu antity<<TheTool .Cost<<endl;
file1.read(rein terpret_cast<ch ar*>(&TheTool), sizeof(record)) ;
}
}[/code]
I want to save it to a binary file and if I exit the program and I open it again, the previous input will still be there. But when I run the program the second time, and read its contents, the name of the tool is replaced with my new input and I don't know why or how to stop this from happening. Also, when the tool identification number is 1,2,3,... (input in proper order), it reads ok(except the name) and runs until it reaches its end. But when the number is in 1,2,3,5 (there's no 4 so not in proper order of input), it will encountered an error. But I want to be able to do that. Please help... Thanks!
(Sorry for the wrong grammar and not nice explanation, i'm not good in english hehe.. thank you!)
^.^
I am making a program but I can't continue because there's something wrong...
[code=cpp]#include<iostre am>
#include<string >
#include<fstrea m>
using namespace std;
struct record{
int IdentificationN umber;
string Name;
int Quantity;
double Cost;
};
main(){
record TheTool;
fstream file("hardware. dat", ios::in|ios::ou t|ios::binary);
if(!file){
cerr<<"File could not be opened or created"<<endl;
exit(1);
}
cout<<"I Will Add Something To The File"<<endl;
cout<<"Enter tool ID Number: ";
cin>>TheTool.Id entificationNum ber;
cout<<"Enter tool name: ";
cin.ignore();
getline(cin, TheTool.Name);
cout<<"Enter Quantity: ";
cin>>TheTool.Qu antity;
cout<<"Enter Cost: ";
cin>>TheTool.Co st;
file.seekp((The Tool.Identifica tionNumber-1)*sizeof(recor d));
file.write(rein terpret_cast<co nst char *> (&TheTool), sizeof(record)) ;
file.close();
fstream file1("hardware .dat", ios::in|ios::ou t|ios::binary);
file1.seekg(0);
file1.read(rein terpret_cast<ch ar*>(&TheTool), sizeof(record)) ;
while(file1.goo d()){
cout<<TheTool.I dentificationNu mber<<TheTool.N ame<<TheTool.Qu antity<<TheTool .Cost<<endl;
file1.read(rein terpret_cast<ch ar*>(&TheTool), sizeof(record)) ;
}
}[/code]
I want to save it to a binary file and if I exit the program and I open it again, the previous input will still be there. But when I run the program the second time, and read its contents, the name of the tool is replaced with my new input and I don't know why or how to stop this from happening. Also, when the tool identification number is 1,2,3,... (input in proper order), it reads ok(except the name) and runs until it reaches its end. But when the number is in 1,2,3,5 (there's no 4 so not in proper order of input), it will encountered an error. But I want to be able to do that. Please help... Thanks!
(Sorry for the wrong grammar and not nice explanation, i'm not good in english hehe.. thank you!)
Comment