【发布时间】:2016-02-07 22:22:08
【问题描述】:
我在两件事上遇到了麻烦,首先在我选择 C 的 if 语句中,它应该从文件中获取选定的记录并更改其内容,但由于某种原因文件内容不会更改,它不起作用。同样在选项 D 中,我需要它能够读取并添加文件中的所有数量和销售成本,并将它们加在一起然后显示它们。我什至不知道从哪里开始。我知道我需要访问文件中的信息,但只有具有如此所述数据的行,然后将它们加在一起并将它们保存在要显示的变量中,但是我怎样才能只访问文件中具有该数据的行?
#include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
using namespace std;
struct Info
{
// create inventory items info
string ItemDescription;
int Quantity;
double WholesaleCost;
double RetailCost;
string Date;
};
int main()
{
//make instance of info and a variable for user selection
Info Item;
char choice;
// set up functions
long byteNum(int);
void showRec(Info);
void changeRec(Info);
//open file
fstream inventory("inventory.dat", ios::out | ios::in | ios::binary);
// loop for user selection
do
{
cout << "press 'A' to add files\n";
cout << "press 'D' to display files\n";
cout << "press 'C' to Change files\n";
cout << "press 'G' to Generate a record\n";
cout << "or press 'Q' to quit\n";
cin >> choice;
//if add files get record info put it into struct and save it tofile
if (choice == 'A' || choice == 'a')
{
cout << "enter Item description\n";
cin >> Item.ItemDescription;
cout << "enter quantity on hand\n";
cin >> Item.Quantity;
cout << "enter whole sale cost\n";
cin >> Item.WholesaleCost;
cout << "enter Item retail cost\n";
cin >> Item.RetailCost;
cout << "enter date added to inventory\n";
cin >> Item.Date;
cout << "data added\n";
inventory.write(reinterpret_cast<char *>(&Item), sizeof(Item));
}
//display record
else if (choice == 'D' || choice == 'd')
{
int recordChoice;
// get which record user wants
cout << "enter which record number you want?";
cin >> recordChoice;
// display record info
cout << " here is record " << recordChoice << endl;
inventory.seekg(byteNum(recordChoice), ios::beg);
inventory.read(reinterpret_cast<char *>(&Item), sizeof(Item));
showRec(Item);
}
// change record info
else if (choice == 'C' || choice == 'c')
{
int recordChoice;
//get which record user wants to change
cout << "enter which record number you want to change?";
cin >> recordChoice;
// change struct info and save it over old record in the file
changeRec(Item);
inventory.seekp(byteNum(recordChoice), ios::beg);
inventory.write(reinterpret_cast<char *>(&Item), sizeof(Item));
}
else if (choice == 'G' || choice == 'g')
{
cout << "D";
}
} while (choice != 'Q' || choice != 'q');
inventory.close();
return 0;
}
long byteNum(int recNum)
{
// get record selection number
return sizeof(Info) * recNum;
}
void showRec(Info record)
{
// display record info
cout << record.ItemDescription << endl;
cout << record.Quantity << endl;
cout << record.WholesaleCost << endl;
cout << record.RetailCost << endl;
cout << record.Date << endl;
}
void changeRec(Info record)
{
// change record info in struct
cout << "enter new Item description\n";
cin >> record.ItemDescription;
cout << "enter new quantity on hand\n";
cin >> record.Quantity;
cout << "enter new whole sale cost\n";
cin >> record.WholesaleCost;
cout << "enter new Item retail cost\n";
cin >> record.RetailCost;
cout << "enter new date added to inventory\n";
cin >> record.Date;
cout << "data added\n";
}
【问题讨论】:
-
您不能让
std::string成员被简单地从/序列化到二进制文件中。 -
请仔细阅读
std::tolower和std::toupper,这样您就可以减少比较次数(或者更好的是,cases 在switch语句中的数量)。 -
在互联网上搜索“C++ 序列化技术字符串”。类到文件的输入和输出称为序列化。有很多文章描述了如何处理包含
std::string和std::vector等指针的数据类型。
标签: c++ struct iostream binaryfiles records