C++ 为什么我会得到一个“;“没有匹配功能”;打开文件名为std::string的fstream时出错?
我试图编写一个程序,从一个文件中读取字符串列表,检查第二个文件中缺少的字符串,并将它们打印到屏幕上。但是,当我尝试编译时,我当前遇到了一个错误。下面是我试图编译代码时遇到的错误。谢谢你的帮助 代码如下:C++ 为什么我会得到一个“;“没有匹配功能”;打开文件名为std::string的fstream时出错?,c++,C++,我试图编写一个程序,从一个文件中读取字符串列表,检查第二个文件中缺少的字符串,并将它们打印到屏幕上。但是,当我尝试编译时,我当前遇到了一个错误。下面是我试图编译代码时遇到的错误。谢谢你的帮助 代码如下: #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; ifstream IN_FILE; ofstrea
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
_文件中的ifstream;
流式输出文件;
int main(){
int k=0;
int m=0;
int n=0;
字符串a[5000];
字符串b[5000];
字符串文件名;
很好;
//打开文件并检查条目是否有效
Cuth此构造适用于标准C++ 11和较新编译器:
std::string filename;
//...
IN_FILE.open(filename);
<>这基本上是你现在的代码,但是,上面的代码将不能与C++ 11标准编译器(C++ 98, 03)一起使用。
如果使用标准的C++ 11编译器编译,则上面的代码应该是:
std::string filename;
//...
IN_FILE.open(filename.c_str()); // Note the null-terminated const char *
<>基本上,代码:<代码>开放代码>代码>在C++ 11之前不存在。在C++ 11之前,必须使用C样式、空终止字符串指定名称。< /P>
<> P>这样,你看到的错误是因为你在C++ 11之前编译了一个C++版本。关于你要解决的问题,注意到你可能会因为声明那些大的<代码>字符串数组而得到堆栈溢出问题。输入这些值,然后调用std::set_difference
,代码量将减少到大约5或10行代码。
std::string filename;
//...
IN_FILE.open(filename.c_str()); // Note the null-terminated const char *