C 程序处理两个文件,而不是一个文件
C++ Program process two files and not one
因此,对于分配,我必须读取文件并计数其行,单词和字符。问题是我编写的程序将读取两个文件,但不是一个文件,该程序将文本文件视为无法打开并将其发送到其他语句的内容。我觉得也许我把东西弄乱了,但是我觉得很奇怪,它可以读两个文件而不是一个文件。代码:
#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;
}
}
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 挂起和取消挂起一个文件DLL
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何打开并写入一个名称取自C++中字符串的文件
- 构建一个由C和C++文件组成的库
- c++多进程编写一个唯一的文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何将class.cpp和class.hpp编译为一个.o文件
- 写入文件会产生一个空文件或根本没有文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- Pantheios 不使用 MFC 应用程序写入一个文件(一个 exe + 两个 DLL)
- 在C++中一次读取一个文件一个int
- 输入/输出文件(一个字母的大写)
- 如何使用两个预编译的头文件.一个用于托管代码,另一个用于非托管代码.xxx.PCH不是一个有效的预编译头文件