2017-02-15 57 views
-1

因此,對於一個任務,我必須讀取一個文件並計算它的行​​,字和字符。問題是我寫的程序會讀取兩個文件,但不是一個,程序會將文本文件視爲無法打開併發送到else語句。我覺得也許我搞砸了一些東西,但我覺得很奇怪它可以讀取兩個文件而不是一個文件。 代碼:C++程序處理兩個文件而不是一個

#include <iostream> 
#include <fstream> // for file-access 
#include <iomanip> 

using namespace std; 
int main(int argc, char* argv[]) 
{ 
    if (argc > 1){} 
    else 
    { 
     cout << "File anInvalidFileName is not found" << endl; 
     return -1; 
    } 
    ifstream infile(argv[1]); 

    if (infile.is_open() && infile.good()) 
    { 
     string line1 = ""; 
     int countline1 = 0; 
     int charcount1 = 0; 
     char space1; 
     int countspace1 = 0; 
     int empty1 = 0; 
     while (getline(infile, line1)) 
     { 
      if (line1.empty()) 
      { 
       empty1++; 
      } 
      countline1++; 
      charcount1 += line1.length() + 1; 
      for (int i = 0; i < line1.length(); i++) 
      { 
       if (line1[i] == ' ') 
       { 
        countspace1++; 
       } 
      } 
     } 
     countspace1 = (countline1 - empty1) + countspace1; 
     ifstream infile(argv[2]); //open the file 
     if (infile.is_open() && infile.good()) 
     { 
      string line2 = ""; 
      int countline2 = 0; 
      int charcount2 = 0; 
      char space2; 
      int countspace2 = 0; 
      int empty2 = 0; 
      while (getline(infile, line2)) 
      { 
       if (line2.empty()) 
       { 
        empty2++; 
       } 
       countline2++; 
       charcount2 += line2.length() + 1; 
       for (int i = 0; i < line2.length(); i++) 
       { 
        if (line2[i] == ' ') 
        { 
         countspace2++; 
        } 
       } 
      } 
      countspace2 = (countline2 - empty2) + countspace2; 
      int countline = 0; 
      int countspace = 0; 
      int charcount = 0; 
      countline = countline1 + countline2; 

      countspace = countspace1 + countspace2; 

      charcount = charcount1 + charcount2; 
      cout << setw(12) << countline1; 
      cout << setw(12) << countspace1; 
      cout << setw(12) << charcount1 << " "; 
      cout << argv[1] << endl; 
      cout << setw(12) << countline2; 
      cout << setw(12) << countspace2; 
      cout << setw(12) << charcount2 << " "; 
      cout << argv[2] << endl; 
      cout << setw(12) << countline; 
      cout << setw(12) << countspace; 
      cout << setw(12) << charcount << " "; 
      cout << "totals" << endl; 
     } 
     else 
     { 
      cout << "error" << endl; 
     } 
     return 0; 
    } 
    else 
    { 
     cout << "error" << endl; 
    } 
} 

錯誤輸出是我加入正好看到其中一個文件被髮送到時失敗。它確實轉到了else,因爲當我運行程序時,它會打印出錯誤。至於輸入,教授提供了程序運行時自動運行的測試用例。 我覺得這可能是一個簡單的錯誤,也許我想知道argv是如何工作的,但任何幫助都會受到歡迎。如果需要更多信息,我會嘗試添加它。

+0

請正確縮進代碼並正確解釋你在做什麼以及什麼時候沒有做。例如,你給它的參數。調試器也是第一步用來查看發生了什麼以及你得到什麼參數。文件名中的空格? –

+0

不幸的是,它看起來像你的鍵盤壞了,它的TAB鍵不起作用。結果,顯示的代碼是完全不可讀的。請固定您的鍵盤,然後使用固定的TAB鍵在邏輯上縮進您的代碼,以便實際上可以讀取它並遵循它的操作。 –

+1

適當的縮進將使這個更具可讀性。另外我相信你可以使這個樣本比現在更小。 TIA。 – Borgleader

回答

0

這適用於任何數量的文件輸入。

希望這對你有所幫助。

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

int main(int argc, char *argv[]) { 
    if (argc > 1) { 
    int countline = 0; 
    int countspace = 0; 
    int charcount = 0; 
    for (int i = 1; i < argc; i++) { 
     ifstream infile(argv[i]); // open the file 
     if (infile.is_open() && infile.good()) { 
     string line2 = ""; 
     int countline2 = 0; 
     int charcount2 = 0; 
     char space2; 
     int countspace2 = 0; 
     int empty2 = 0; 
     while (getline(infile, line2)) { 
      if (line2.empty()) { 
      empty2++; 
      } 
      countline2++; 
      charcount2 += line2.length() + 1; 
      for (int i = 0; i < line2.length(); i++) { 
      if (line2[i] == ' ') 
       countspace2++; 
      } 
     } 
     countspace2 = (countline2 - empty2) + countspace2; 
     countline += countline2; 

     countspace += countspace2; 

     charcount += charcount2; 
     cout << setw(12) << countline2; 
     cout << setw(12) << countspace2; 
     cout << setw(12) << charcount2 << " "; 
     cout << argv[i] << endl; 
     cout << setw(12) << countline; 
     cout << setw(12) << countspace; 
     cout << setw(12) << charcount << " "; 
     cout << "totals" << endl; 
     } 
     infile.close(); 
    } 
    } else { 
    cout << "File anInvalidFileName is not found" << endl; 
    return -1; 
    } 
} 
+0

你會爲其他海報編寫代碼嗎? *您的代碼如何解決OP的問題? –

+0

此代碼段可以讀取一個文件以及任意數量的文件。 – hcnak

相關問題