C++ C++;程序处理两个文件而不是一个
所以,对于一个作业,我必须阅读一个文件,并计算它的行、字和字符。问题是,我编写的程序将读取两个文件,但不是一个,程序将文本文件视为无法打开的文件,并发送到else语句。我觉得可能是我弄糟了什么,但我觉得奇怪的是,它可以读取两个文件,而不是一个。 代码:C++ C++;程序处理两个文件而不是一个,c++,c++11,C++,C++11,所以,对于一个作业,我必须阅读一个文件,并计算它的行、字和字符。问题是,我编写的程序将读取两个文件,但不是一个,程序将文本文件视为无法打开的文件,并发送到else语句。我觉得可能是我弄糟了什么,但我觉得奇怪的是,它可以读取两个文件,而不是一个。 代码: #包括 #包含//用于文件访问 #包括 使用名称空间std; int main(int argc,char*argv[]) { 如果(argc>1){} 其他的 { cout这适用于任何数量的文件输入 希望这对你有帮助 #include <
#包括
#包含//用于文件访问
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
如果(argc>1){}
其他的
{
cout这适用于任何数量的文件输入
希望这对你有帮助
#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;
}
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
如果(argc>1){
int countline=0;
int countspace=0;
int charcount=0;
对于(int i=1;i 请正确缩进代码,并准确解释代码工作时和不工作时您正在执行的操作。例如,您为代码提供的参数。调试器也是第一步,用于查看发生了什么以及您在文件名中得到了什么参数。空格?不幸的是,您的键盘好像坏了,其TAB键没有工作。因此,显示的代码完全不可读。请修复键盘,然后使用fixed TAB键对代码进行逻辑缩进,这样实际上就可以读取代码并遵循代码的操作。正确的缩进将使其更可读。此外,我相信您可以使此示例比它小得多当前是.TIA。如果使用VS,按ctrl键ctrl
+k
+d
。要跟进Borgleader的评论:让你的示例只计算行数。这将大大缩短长度。另外:让“错误”字符串唯一总是值得的,即使它只是“错误1”、“错误2”。您会为其他海报编写代码吗?您的代码如何解决OP的问题?此代码段可以读取一个文件以及任意数量的文件。
#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;
}
}