0

I get this error when I try running the program.What might be the problem as the code is correct as far as I can see.

Here is the error

std::basic_fstream::basic_fstream(std::string&, const openmode&)'

Here is the code

#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;
}

2 Answers 2

1

You could change

fstream inFile(fileName, fstream::in); 

to

fstream inFile(fileName.c_str(), fstream::in);
Sign up to request clarification or add additional context in comments.

Comments

0

Although C++11 defines a std::fstream constructor that accepts a std::string as input, Microsoft's implementation of std::fstream apparently does not:

https://msdn.microsoft.com/en-us/library/a33ahe62.aspx#basic_fstream__basic_fstream

You will have to use the std::string::c_str() method to pass the filename:

fstream inFile(fileName.c_str(), fstream::in);

That being said, consider using std::ifstream instead:

ifstream inFile(fileName.c_str());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.