C ++程序处理两个文件而不是一个

时间:2017-02-15 15:33:52

标签: c++ c++11

因此,对于作业,我必须阅读文件并计算其行,单词和字符。问题是我编写的程序会读取两个文件而不是一个,程序会将文本文件视为无法打开的文件并发送到else语句。我觉得也许我弄乱了一些东西,但我觉得奇怪的是它可以读取两个文件而不是一个。 代码:

#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,因为当我运行程序时它打印出错误。至于输入,prof提供了在程序运行时自动运行的测试用例。 我觉得这可能是一个简单的错误,也许我很想知道argv是如何工作的,但任何帮助都会受到欢迎。如果需要更多信息,我会尝试添加它。

1 个答案:

答案 0 :(得分: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;
  }
}