2017-02-15 4 views
-1

割り当てのために、ファイルを読み込んでその行、単語、および文字を数えなければなりませんでした。問題は、私が書いたプログラムが2つのファイルを読み上げるが1つではないということだ。プログラムはテキストファイルを開けないものとして見て、else文に送る。私は何かを混乱させたかもしれないが、私はそれが2つのファイルを読むことができ、1つではないことが奇妙であると感じている。 コード:C++ 2つのファイルを処理するプログラムで、1つではない

#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; 
    } 
} 

エラー出力には、私は1つのファイルが、それが失敗したときに送信されます場所だけ見るために追加のものです。私はプログラムを実行すると、エラーを出力するので、それは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

このコードスニペットは、1つのファイルと任意の数のファイルを読み取ることができます。 – hcnak

関連する問題