2016-12-02 4 views
0

こんにちは私は、行数をカウントする1つの関数(それは動作します)の後、charのdosentの仕事がtxtファイルの行と量の数をカウントしようとしていますif私はcharカウンタを単独で使用します。 (私は一つの機能にそれを混在させることができます知っているが、私はこの例では、修正するという大きな問題を持っている)C++ fstream dosent 1関数の後の仕事

メイン:

int main() 
{ 
    ifstream isf("D:\\test.txt", ios_base::in); 
    ofstream osf("D:\\test.txt", fstream::app); 
    //WriteToFile(osf,isf); 
    cout << CountLines(isf)<< endl; 
    cout << CountChar(isf) <<endl; 
    isf.close(); 
    osf.close(); 
    return 0; 
} 

機能:

const int CountLines(ifstream& isf) 
{ 
    int count = 1; 
    char c; 
    while (isf.get(c)) 
    { 
     if (c == '\n') 
      ++count; 
    } 
    return count; 
} 
const int CountChar(ifstream& isf) 
{ 
    int count = 0; 
    char c; 
    while (isf.get(c)) 
    { 
     ++count; 
    } 
    return count; 
} 

txtファイル:

abc 
abc 

出力:

2 
0 
Press any key to continue . . . 

、出力はあなたが第一の関数を呼び出した後に開始位置にストリームをリセットする必要が

2 
7 
Press any key to continue . . . 

答えて

1

次のようになります。

cout << CountLines(isf)<< endl; 
isf.clear(); // Reset stream states like eof() 
isf.seekg(0); // <<<<<<<<<<<<<<<<<< 
cout << CountChar(isf) <<endl; 
+0

どう_ "それそれまだdosent仕事 – Rokni

+0

@Rokni _特に動作しませんか?たぶん 'isf.clear()'を追加すると、ストリームの状態を完全にリセットするのに役立つかもしれません。 –

+0

それは動作しませんが、私は彼らの順序を変更するとき。 ilあなたの投稿を編集し、答えとしてマークしていただきありがとうございます。 – Rokni

関連する問題