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

C++ Program process two files and not one

本文关键字:文件 一个 两个 程序 处理      更新时间:2023-10-16

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

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

我添加了错误输出,只是为了查看一个文件失败时将其发送到何处。它确实转到了其他方面,因为当我运行程序时,它会打印出错误。至于输入,教授提供了在运行程序时自动运行的测试用例。我觉得这可能是一个简单的错误,也许我想念ARGV的工作原理,但是欢迎任何帮助。如果需要更多信息,我将尝试添加它。

这适用于任何数量的文件输入。

希望这对您有帮助。

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