2017-02-15 56 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

相关问题