您好我已经取得了一些进展我将信息从文本文件写入二进制文件,但现在我无法将其从二进制文件显示到控制台。知道我做错了什么。我尝试过很多东西。我一直在网上查找东西。 这是我用于主要目标的代码。
int main(){
ifstream("Input2.txt");
primary.writePrimary();
secondary.writeSecondary();
int input = 0;
// the main function will be used to display the main menu on screen
while (input != 8){
std::cout << "Main Menu" << endl
<< "1---Add a Record" << endl
<< "2---Change a record" << endl
<< "3---Delete a record" << endl
<< "4---Print a record or records" << endl
<< "5---Sell a Title" << endl
<< "6---Sold value" << endl
<< "7---Build binary file" << endl
<< "8---Quit" << endl;
std::cout << "Choose a number: ";
cin >> input;
// here are the cases based on what number is entered
switch (input){
case 1:
addRecord();
break;
case 2:
editRecord();
break;
case 3:
remove();
break;
case 4:
printRecord();
case 5:
sellRecord();
break;
case 6:
soldValue();
break;
case 7:
makefile();
break;
case 8:
primary.writePrimary();
secondary.writeSecondary();
system("exit");
break;
}
}
system("pause");
};
void printRecord(){
int input = 0;
while (input != 7){
std::cout << "1---Print Record alphebetically" << endl
<< "2---Print record reverse alphabetically" << endl
<< "3---Print by title " << endl
<< "4---Print by type" << endl
<< "5---Print by sorted year ascending order" << endl
<< "6---Print Summary" << endl
<< "7---Go back" << endl;
std::cout << "Choose a number: ";
cin >> input;
// here are the cases based on what number is entered
switch (input){
case 1:
show();
break;
case 2:
break;
case 3:
printbyTitle();
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
system("CLS");
return;
break;
}
}
}
void addRecord(){
system("CLS"); //clears screen
fstream fout;
Data inv;
char ch;
fout.open("output.bin", ios::out | ios::binary | ios::app);
do
{
inv = EnterInventory();
//write record to file
fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
system("Pause");
std::cout << "Do you want to add another record? " << endl;
cin >> ch;
} while (tolower(ch) == 'y');
//close the file
fout.close();
if (tolower(ch) == 'n'){
return;
}
}
void editRecord(){
system("CLS");
int x;
Data inv;
fstream fout;
fout.open("output.bin", ios::in | ios::binary);
fout.seekg(0, ios::end);
int count = fout.tellg() / sizeof(inv);
std::cout << "\n There are " << count << " record in the file";
std::cout << "\n Enter Record Number to edit: ";
cin >> x;
fout.seekg((x - 1)*sizeof(inv));
fout.read(reinterpret_cast<char*>(&inv), sizeof(inv));
std::cout << "Record " << x << " has following data: " << endl;
PrintInventory(inv);
fout.seekp((x - 1)*sizeof(inv));
std::cout << "\nEnter data to Modify: " << endl;
inv = EnterInventory();
fout.write(reinterpret_cast <char*>(&inv), sizeof(inv));
}
void sellRecord(){
system("CLS");
Data inv;
fstream fout;
int x;//to hold the record number the user wishes to sell
fout.open("output.bin", ios::in | ios::out | ios::binary);
fout.seekg(0, ios::end);
int count = fout.tellg() / sizeof(inv);
cout << "\n There are " << count << " record in the file";
cout << "\n Enter Record Number to sell: ";
cin >> x;
fout.seekg((x - 1)*sizeof(inv));
fout.read(reinterpret_cast<char*>(&inv), sizeof(inv));
cout << "Selected entry:\n";
PrintInventory(inv);
int yes;
cout << "Is this correct?\n1. Yes\n2. No\n>";
cin >> yes;
if (yes == 1)
{
inv.count--;
Data::sum += inv.price;
Data::sold += 1;
fout.seekp((x - 1) * sizeof(inv));
fout.write((char*)&inv, sizeof(inv));
cout << "SOLD!" << endl;
system("pause");
system("CLS");
return;
}
system("pause");
}
void soldValue(){
system("CLS");
Data inv;
fstream fout;
int x; //to display the value of sold records
fout.open("output.bin", ios::in | ios::out | ios::binary);
fout.seekg(0, ios::end);
int count = fout.tellg() / sizeof(inv);
cout << "\n There are " << count << " record in the file";
cout << "\nThe total value of the " << Data::sold << " records sold are: $" << Data::sum << endl;
system("PAUSE");
}
按标题打印功能只按标题打印
}
void printbyTitle(){
ifstream titl("primary.txt");
if (!titl.is_open()){
std::cout << "file not found" << endl;
return;
}
while (!titl.eof()){
string title;
int key = 0;
std::cout << title << " " << key << endl;
titl >> title >> key;
}
}
这将从列表中删除一行。
void remove()
{
system("CLS");
Data inv;
fstream fout;
int x;//to hold the record number the user wishes to delete
fout.open("output.bin", ios::in | ios::out | ios::binary);
fout.seekg(0, ios::end);
int count = fout.tellg() / sizeof(inv);
std::cout << "\n There are " << count << " record in the file";
std::cout << "\n Enter Record Number to delete: ";
cin >> x;
fout.seekg((x - 1)*sizeof(inv));
fout.read(reinterpret_cast<char*>(&inv), sizeof(inv));
inv.dead_flag = true;
fout.seekp(fout.tellg() - fstream::pos_type(sizeof(Data)));;
fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
fout.close();
}
这是我用来让它在控制台上显示二进制文件信息的函数
void show(){
system("CLS");
Data inv;
fstream fout;
fout.open("output.bin", ios::binary | ios::in);
fout.clear();
fout.read((char*)&inv, sizeof(inv));
int tag = 1;
while (fout.good() && !fout.eof())
{
PrintInventory(inv);
fout.read((char*)&inv, sizeof(inv));
}
fout.close();
system("PAUSE");
}
任何想法我应该做什么?