我在打開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()
不能讀取多個字符串空間。我正在考慮使用子字符串,但我不完全知道如何繼續。你們能幫我嗎?
'c_str()'不關心空格......這裏有什麼問題? – Cornstalks
你的代碼中存在一些明顯的(?)錯誤,它們會阻止它編譯(例如,在最後一行中出現'out'而不是'cout','w'無處定義等等)。請提供工作代碼。除此之外,如果我將您的代碼「修復」到我所知道的最好的代碼,它確實會處理它似乎具有空格的文件名。 –
如何調試一點點......就像'cout <<「關於打開[」<< fname <<「] \ n」;'你知道,確保你的字符串是你的想法。 – paddy