我正在尝试编写一个程序,该程序从文件中读取字符串列表,并检查第二个文件中缺少哪些字符串并将它们打印到屏幕上。但是,我目前在尝试编译时遇到错误。以下是我在尝试编译以及代码时遇到的错误。感谢您的帮助
这是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
ifstream IN_FILE;
ofstream OUT_FILE;
int main() {
int k = 0;
int m = 0;
int n = 0;
string a[5000];
string b[5000];
string filename;
bool good;
//open file and check if valid entry
cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
getline(cin, filename);
IN_FILE.open(filename);
while(!IN_FILE) {
cout << "Sorry the file you entered could not be opened\n";
cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
getline(cin, filename);
IN_FILE.open(filename);
}
//Read every line from file
while(!IN_FILE.eof()) {
getline(IN_FILE, a[k]);
k++;
}
n = k;
k = 0;
IN_FILE.close();
//open file and check if valid entry
cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
getline(cin, filename);
IN_FILE.open(filename);
while(!IN_FILE) {
cout << "Sorry the file you entered could not be opened\n";
cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
getline(cin, filename);
IN_FILE.open(filename);
}
//Read every line from file
while(!IN_FILE.eof()) {
getline(IN_FILE, b[k]);
k++;
}
m = k;
k = 0;
//Compare the arrays and print all elements is array a that are not in array b
for (int i = 0; i < n; i++) {
int j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;
if (j == m)
cout << a[i] << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
checkTester.cpp:25:26: 错误:没有匹配的函数调用'std::basic_ifstream<char>::open(std::__cxx11::string&)'
IN_FILE.open(文件名);
此构造适用于标准 C++ 11 和更新的编译器:
std::string filename;
//...
IN_FILE.open(filename);
Run Code Online (Sandbox Code Playgroud)
这基本上就是您现在拥有的代码。但是,上述内容不适用于标准前的 C++ 11 编译器 (C++ 98, 03)。
如果您使用预标准的 C++ 11 编译器进行编译,那么上面的代码应该是:
std::string filename;
//...
IN_FILE.open(filename.c_str()); // Note the null-terminated const char *
Run Code Online (Sandbox Code Playgroud)
基本上,C++ 11 之前不存在的std::string版本。open在 C++ 11 之前,您必须使用 C 样式的空终止字符串指定名称。
因此,您看到的错误是因为您在 C++ 11 之前的 C++ 版本中进行编译。
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |