我在打开名称中有空格的 C++ 文件时遇到问题。例如,打开文件read me.txt.
这是我到目前为止的代码,涉及一个读取文件并将字数输出到控制台的命令:
string choice, word, fname;
ifstream input;
int l, count = 0;
if(choice == "wc" || choice == "WC" || choice == "Wc")
{
    getline(cin, fname);
    input.open(fname.c_str());
    cout << fname << endl;
    if(input.fail())
    {
        cerr << " Error: failed to open the file: " << fname << endl;
        input.clear();
    }
    else
    {
        w = 0;
        while (input >> word)
           w++;
        input.close();
        count = w;
        cout << fname << " has a Word Count of: " << count << " words \n" << endl;
    }
}
我知道流函数c_str()在空格后不能读取多个字符串。我正在考虑使用子字符串,但我不完全确定如何进行。你们能帮帮我吗?