gpt4 book ai didi

c++ - 如何从包含空格的文件中读取字符串 C++

转载 作者:行者123 更新时间:2023-11-27 23:35:41 25 4
gpt4 key购买 nike

我写了一段代码来搜索文件中的字符串。实际上,我把我的数据放在我的文件中是这样的:

alex booth,15,1(带空格、年龄、身份证的全名)。

在文件中保存数据没有任何问题,在该文件中搜索字符串时遇到了一个大问题。

例如,我想在文件中搜索像“alex booth”这样的名称,但似乎搜索无法找到该词,尽管该词可用。我猜想用空格保存名称有问题,但我不知道我应该怎么做才能修复它。事实上,我不知道我们如何才能搜索其中包含空格的名称。

这是我的代码:

using namespace std;
struct product{
string name;
int price, id;
};
int main() {
product myProduct;
ofstream productList("product.csv", ios::app | ios::out);
getline(cin, myProduct.name);
cin >> myProduct.price;
cin >> myProduct.id;
productList << myProduct.name << ',' << myProduct.price << ',' << myProduct.id << endl;
productList.close();
cout << "----" << endl;
system("pause");
ifstream checkList("product.csv");
string dummy;
string check;
cin.ignore();
getline(cin, check);
while (!checkList.eof()) {
while (checkList >> myProduct.name >> myProduct.price >> myProduct.id) {
if (myProduct.name == check) {
cout << "Product Name: " << myProduct.name <<
" - Product's Price: " << myProduct.price <<
" - ID Number: " << myProduct.id << endl;
}
}
}
checkList.close();
return 0;
}

最佳答案

当您从文件中读取行时,您根本没有考虑逗号,然后 operator>> 在您的内部 while 循环中失败。

此外,your outer while loop is wrong ,也是。

尝试更像这样的东西:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <limits>
#include <cstdlib>
using namespace std;

struct product{
string name;
int price, id;
};

int main() {
product myProduct;

cout << "Enter product name: ";
getline(cin, myProduct.name);

cout << "Enter product price: ";
cin >> myProduct.price;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Enter product id: ";
cin >> myProduct.id;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

ofstream productList("product.csv", ios::app);
productList << myProduct.name << ',' << myProduct.price << ',' << myProduct.id << std::endl;
productList.close();

cout << "----" << endl;
system("pause");
cin.ignore();

cout << "Enter name to look for: ";
string check;
getline(cin, check);

ifstream checkList("product.csv");
string line;
char comma;

while (getline(checkList, line)) {
istringstream iss(line);
if (getline(iss, myProduct.name, ',') && (iss >> myProduct.price >> comma >> myProduct.id) && (comma == ',')) {
if (myProduct.name == check) {
cout << "Product Name: " << myProduct.name <<
" - Product's Price: " << myProduct.price <<
" - ID Number: " << myProduct.id << endl;
}
}
}
checkList.close();

return 0;
}

或者:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <limits>
#include <cstdlib>
using namespace std;

struct product{
string name;
int price, id;
};

istream& getInt(istream &in, int &value)
{
string str;
if (getline(in, str, ',')) {
try {
value = stoi(str);
}
catch (...) {
in.setstate(ios::failbit);
}
}
return in;
}

int main() {
product myProduct;

cout << "Enter product name: ";
getline(cin, myProduct.name);

cout << "Enter product price: ";
cin >> myProduct.price;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Enter product id: ";
cin >> myProduct.id;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

ofstream productList("product.csv", ios::app);
productList << myProduct.name << ',' << myProduct.price << ',' << myProduct.id << std::endl;
productList.close();

cout << "----" << endl;
system("pause");
cin.ignore();

cout << "Enter name to look for: ";
string check;
getline(cin, check);

ifstream checkList("product.csv");
string line;

while (getline(checkList, line)) {
istringstream iss(line);
if (getline(iss, myProduct.name, ',') && getInt(iss, myProduct.price) && getInt(iss, myProduct.id)) {
if (myProduct.name == check) {
cout << "Product Name: " << myProduct.name <<
" - Product's Price: " << myProduct.price <<
" - ID Number: " << myProduct.id << endl;
}
}
}
checkList.close();

return 0;
}

关于c++ - 如何从包含空格的文件中读取字符串 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59381566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com