我有我的这个学校项目,需要一些帮助来进行故障排除;我已经调试了它是否进入while(prod_file.good())循环,但确实如此,它只是无法在文件中找到现有的字符串。
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define PROD_SIZE 22
class PRODUCT {
public:
string NAME;
string QUANTITY;
} product;
int main() {
int diff;
string length;
char answer = 0;
string line;
bool foundAndReplaced = false;
cout << product.NAME.length() << endl;
cout << product.NAME;
fstream prod_file("data/available.dat", ios::out | ios::app);
do {
if(prod_file.is_open()) {
cout << "Molq vavedete imeto na produkta: \n";
getline(cin, product.NAME);
if(product.NAME.length() < 30) {
diff = 30 - product.NAME.length();
for(int i = 1; i < diff; i++){
product.NAME.append(".");
}
}
else if(product.NAME.length() > 30) {
cout << "Product name cannot exceed 30 characters.";
return 0;
}
//
cout << "Molq vavedete kolichestvoto na produkta: \n";
getline(cin, product.QUANTITY);
size_t pos;
while(prod_file.good()) {
cout << "asd\n";
getline(prod_file, line);
pos = line.find(product.NAME);
cout << "stignah\n";
if(pos != string::npos) {
prod_file << product.NAME;
prod_file << "-" << product.QUANTITY << "\n";
}
}
}
else {
cout << "Error: File inaccessible. (0x1)";
return 0;
}
cout << "Do you want to add more products? Y/N \n\n";
answer = 0;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
answer = _getch();
}
}
while(answer == 'Y' || answer == 'y');
prod_file.close();
cout << "Entries added." << "\n\n";
return 0;
}
的想法
size_t pos;
while(prod_file.good()) {
cout << "asd\n";
getline(prod_file, line);
pos = line.find(product.NAME);
cout << "stignah\n";
if(pos != string::npos) {
prod_file << product.NAME;
prod_file << "-" << product.QUANTITY << "\n";
}
}
是要检查文件中的某个字符串,如果没有找到要添加到文件中的字符串,如果找到了,则不会添加它。知道为什么它不能通过if吗?
答案 0 :(得分:1)
您打开prod_file
仅用于输出,而不是输入,因此std::getline()
不能读取任何内容。
您不应尝试使用单个流来读取和写入同一文件。打开原始文件以供一个流读取,然后创建一个新文件供一个独立的流写入。使用第一个流扫描原始文件中的现有条目,并使用第二个流将条目写入新文件。完成后,根据需要用新文件替换原始文件。
尝试这样的事情:
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <cstdio>
using namespace std;
struct Product {
string Name;
int Quantity;
};
int main() {
string line;
char answer;
bool found, added = false, copyLines = true;
Product product;
ifstream prod_file("data/available.dat");
ofstream prod_file_new("data/available_new.dat", ios_base:::trunc);
do {
if (!prod_file || !prod_file_new) {
cerr << "Error: File inaccessible." << endl;
return 0;
}
do {
cout << "Molq vavedete imeto na produkta: " << endl;
if (!getline(cin, product.Name)) {
if (!cin.eof()) {
cerr << "Error: user input failure." << endl;
return 0;
}
break;
}
if (product.Name.length() <= 30) {
break;
}
cerr << "Product name cannot exceed 30 characters." << endl;
}
while (true);
if (cin.eof()) {
break;
}
if (product.Name.length() < 30) {
product.Name.append(30 - product.Name.length(), '.');
}
//
found = false;
do {
if (!getline(prod_file, line)) {
if (!prod_file.eof()) {
cerr << "Error: input file failure." << endl;
return 0;
}
break;
}
if (copyLines) {
if (!(prod_file_new << line << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
}
found = (line.find(product.Name) != string::npos);
}
while (!found);
if (!found) {
cout << "Molq vavedete kolichestvoto na produkta: " << endl;
if (!(cin >> product.Quantity)) {
cerr << "Error: user input failure." << endl;
return 0;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (!(prod_file_new << product.Name << "-" << product.Quantity << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
added = true;
}
cout << "Do you want to add another product? Y/N" << endl << endl;
cin >> answer;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
cin >> answer;
}
if (answer != 'Y' && answer != 'y')
break;
if (!prod_file.seekg(0)) {
cerr << "Error: input file failure." << endl;
return 0;
}
copyLines = false;
}
while (true);
prod_file.close();
prod_file_new.close();
if (added) {
if (remove("data/available.dat") == 0) {
if (rename("data/available_new.dat", "data/available.dat") == 0) {
cout << "Entries added." << endl << endl;
}
else {
cerr << "Error: renaming available_new.dat to available.dat" << endl << endl;
}
}
else {
cerr << "Error: removing available.dat" << endl << endl;
}
}
else {
remove("data/available_new.dat");
cout << "No Entries added." << endl << endl;
}
return 0;
}
或者,将现有文件读入内存,然后根据需要搜索并追加到文件中,然后仅在添加了条目的情况下写出新文件,例如:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <cstdio>
using namespace std;
struct Product {
string Name;
int Quantity;
};
int main() {
string line;
char answer;
bool found, added = false;
Product product;
vector<Product> products;
ifstream prod_file("data/available.dat");
if (!prod_file) {
cerr << "Error: input file inaccessible." << endl;
return 0;
}
while (getline(prod_file, line)) {
istringstream iss(line);
if (!(getline(iss, product.Name, '-') && (iss >> product.Quantity))) {
cerr << "Error: input file malformed." << endl;
return 0;
}
products.push_back(product);
}
if (!prod_file) {
cerr << "Error: input file failure." << endl;
return 0;
}
prod_file.close();
do {
do {
cout << "Molq vavedete imeto na produkta: " << endl;
if (!getline(cin, product.Name)) {
if (!cin.eof()) {
cerr << "Error: user input failure." << endl;
return 0;
}
break;
}
if (product.Name.length() <= 30) {
break;
cerr << "Product name cannot exceed 30 characters." << endl;
}
while (true);
if (cin.eof()) {
break;
}
if (product.Name.length() < 30) {
product.Name.append(30 - product.Name.length(), '.');
}
//
found = std::find_if(products.begin(), products.end(),
[&](const Product &p){ return (p.Name == product.Name); }
) != products.end();
if (!found) {
cout << "Molq vavedete kolichestvoto na produkta: " << endl;
if (!(cin >> product.Quantity)) {
cerr << "Error: user input failure." << endl;
return 0;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
products.push_back(product);
added = true;
}
cout << "Do you want to add another product? Y/N" << endl << endl;
cin >> answer;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
cin >> answer;
}
if (answer != 'Y' && answer != 'y')
break;
}
while (true);
if (added) {
ofstream prod_file_new("data/available_new.dat", ios_base:::trunc);
if (!prod_file_new) {
cerr << "Error: output file inaccessible." << endl;
return 0;
}
for (auto &p : products) {
if (!(prod_file_new << p.Name << "-" << p.Quantity << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
}
prod_file_new.close();
if (remove("data/available.dat") == 0) {
if (rename("data/available_new.dat", "data/available.dat") == 0) {
cout << "Entries added." << endl << endl;
}
else {
cerr << "Error: renaming available_new.dat to available.dat" << endl << endl;
}
}
else {
cerr << "Error: removing available.dat" << endl << endl;
}
}
else {
cout << "No Entries added." << endl << endl;
}
return 0;
}