名前にスペースが含まれるファイルを C++ で開く際に問題があります。たとえば、ファイルを開くにはread me.txt
.
これは、ファイルを読み取り、単語数をコンソールに出力する 1 つのコマンドを含む、これまでのコードです。
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()
ストリーム関数は、スペースの後に複数の文字列を読み取ることができないことを知っています。部分文字列を使用することを考えていましたが、続行する方法が完全にはわかりません。皆さん、私を助けてくれませんか?