当我尝试运行程序时出现此错误。可能是问题,因为代码是正确的,就我所见。
这是错误
std :: basic_fstream :: basic_fstream(std :: string&,const openmode&)'
以下是代码
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string fileName;
int frequencyArray[26];
char character;
for (int i = 0; i < 26; i++)
frequencyArray[i] = 0;
cout << "Please enter the name of file: ";
getline(cin, fileName);
fstream inFile(fileName, fstream::in); // to read the file
if (inFile.is_open())
{
while (inFile >> noskipws >> character)
{
// if alphabet
if (isalpha(character))
{
frequencyArray[(int)toupper(character) - 65]++;
}
}
inFile.close();
cout << "Letter frequencies are as: " << endl;
for (int i = 0; i < 26; i++)
{
cout << (char)(i + 65) << " = " << frequencyArray[i] << endl;
}
}
else
{
cout << "Invalid File. Exiting...";
}
return 0;
}
答案 0 :(得分:1)
你可以改变
?
到
fstream inFile(fileName, fstream::in);
答案 1 :(得分:0)
虽然C ++ 11定义了一个std::fstream
构造函数,它接受std::string
作为输入,但Microsoft的std::fstream
实现显然没有:
https://msdn.microsoft.com/en-us/library/a33ahe62.aspx#basic_fstream__basic_fstream
您必须使用std::string::c_str()
方法传递文件名:
fstream inFile(fileName.c_str(), fstream::in);
话虽如此,请考虑改为使用std::ifstream
:
ifstream inFile(fileName.c_str());