我嘗試創建一個C++程序,讀取一個文件,並計算以下具體準則的文件中的行數,字數和字符數:的I/C++文件的Ø
- 用途的std ::函數getline( )逐行讀取輸入。
- 統計所有字符,包括分隔行的換行符。 cin.eof()的返回值可用於確定一行是否被換行符終止。
- 每個單詞由空格或製表符分隔(「\ t」)。
- first_of()和first_not_of()查找下一個單詞的開始。
唯一的問題是,我不知道如何做到這一點。但是,我設法使用getline()來計算行數。但之後,不知道。如果你能指出我正確的方向,並告訴我如何做到這一點(我是一個視覺學習者),我將不勝感激!
繼承人我得到了什麼至今(不太多,我知道):
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
int numLines = 0;
ifstream file ("horton.txt");
if (file.is_open()){
while (getline (file,line)){
++numLines;
}
file.close();
cout <<numLines<<endl;
}
else cout << "Unable to open file";
}
有人嗎?我在這裏需要一些幫助,因爲我畫空白。 – a22asin 2014-11-03 22:57:16
那麼,如何知道一個字符串是否包含空格或製表符等字符? – 2014-11-03 23:05:13
這個任務,假設有。 – a22asin 2014-11-03 23:08:39