我是C ++新手,对文件读/写有一些误解......
我有一个.txt数据文件,其中包含有关街道的信息,即街道,住宅号码和公寓数量
我有一项任务是从文件中读取数据,添加新数据,删除不需要的内容并对某些街道上的单位数进行一些计算
对于程序工作期间的数据存储,我创建了一个struct的向量:
struct homeAdressBook {
std::string street;
char homeNumber[255];
int flats;
};
std::vector<homeAdressBook> HAB;
程序代码如下:
std::fstream document;
document.open("name.txt");
if (!document) {
std::ofstream document;
document.open("name.txt");
}
if (!document)
std::cout << "File open error" << std::endl;
else {
//Here (while loop) I want to read data from a file and add to my vector of struct for further usage, it partly works somehow but this point is unclear for me
while (!document.eof()) {
HAB.push_back(homeAdressBook());
std::getline(document, HAB[i].street);
document >> HAB[i].street;
document >> HAB[i].homeNumber;
document >> HAB[i].flats;
i++;
}
document.close();
do {
switch (menu()) {
case 0: {
document.close();
exit = false;
break;
}
//Here (case 1) I add new data and write it in file, here I have no problems at the moment
case 1: {
document.open("name.txt", std::ios::app);
HAB.push_back(homeAdressBook());
std::cin.ignore();
std::cout << "Enter the address: ";
std::getline(std::cin, HAB[i].street);
std::cout << "Enter home number: ";
std::cin.getline(HAB[i].homeNumber, 255);
std::cout << "Enter number of flats: ";
std::cin >> HAB[i].flats;
document << HAB[i].street << HAB[i].homeNumber << HAB[i].flats << std::endl;
document.close();
i++;
break;
}
//Here (case 2) I want to read data from a file, it reads data except first line of data
case 2: {
int total = i;
std::cout << std::setw(20) << "Street" << std::setw(20) << "Home Number" << std::setw(20) << "Number of flats" << std::endl;
document.open("name.txt", std::ios::in);
for (int j = 0; j < total; j++) {
std::cout << std::setw(20) << HAB[j].street << std::setw(20) << HAB[j].homeNumber << std::setw(20) << HAB[j].flats << std::endl;
}
document.close();
break;
}
//Here (case 3) data should be deleted from a file if necessary
case 3: {
document.open("name.txt", std::ios::out | std::ios::in);
int strDelete = 0;
std::cout << "0. Exit to main menu" << std::endl;
for (i = 0; i < HAB.size(); i++) {
std::cout << i + 1 << ". " << HAB[i].street << std::endl;
}
std::cout << "\nType number of street to delete: ";
std::cin >> strDelete;
if (strDelete)
i--;
else break;
HAB.erase(HAB.begin() + (strDelete - 1));
document.close();
break;
}
}
} while (exit);
}
system("pause");
return 0;
}
.txt文件中的数据是这样写的:
aaa street 10 50
aaa street 10 45
bbb street 1 10
ccc street 1 12
ccc street 1/10 25
.txt文件中的每一行都属于struct元素的[i] -s向量 我无法理解在程序工作期间如何正确地将.txt文件中的数据添加到我的结构向量中(街道名称最多可以包含5或6个单词以及程序如何分离是否是街道名称或者是家庭数量或公寓数量)
答案 0 :(得分:1)
我建议您为结构重载operator>>
:
struct homeAdressBook
{
std::string column1;
std::string street;
std::string homeAddress;
int flats;
friend std::istream& operator>>(std::istream& input, homeAddressBook& address);
};
std::istream& operator>>(std::istream& input, homeAddressBook& address)
{
input >> address.column1;
input >> address.street;
input >> address.homeAddress;
input >> address.flats;
return input;
}
以上将允许您将输入过程更改为:
std::vector<homeAddressBook> database;
homeAddressBook record;
//...
std::ifstream data_file("addresses.txt");
while (data_file >> record)
{
database.push_back(record);
}
备注
document
作为输出流(ostream
)并使用
输出流上的getline
。std::vector
作为本书。 编辑1:
operator>>
中带有“地址”的前缀成员。