所以我正在编写一个程序,其中多个文件都采用命令行参数。例如,我在命令提示符下输出program.exe file1.txt file2.txt。由于某种原因,我的输出没有显示正确的结果。当我输入两个文件名时,它只显示第一个文件名两次。我附上了一个截图。它也没有计算字符或字符。我认为它必须对局部或全局变量做一些事情,但我已经尝试过将它们放在一处。或许我对自己的错误视而不见。我会很感激任何建议。
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int i;
char ch;
int charcounter=0;
int wordcounter=0;
int linecounter=0;
int main(int argc, char* argv[])
{
for ( i = 1; i < argc; i++ ){
if (argc<2){
cout<<"you did not enter enough arguments"<<endl;
}
else {
string filename;
ifstream thefile;
filename=argv[i];
thefile.open(argv[i]);
if (thefile.is_open() && thefile.good())
{
while (ch!= EOF){
charcounter++;
if (ch ==' ')
wordcounter++;
else if (ch =='\n')
linecounter++;
ch=thefile.get();
cout<<setw(12)<<"Lines"<<' '<<linecounter;
cout<<' ';
cout<<setw(12)<<"Words"<<' '<<wordcounter;
cout<<' ';
cout<<setw(12)<<"Characters"<<' '<<charcounter;
cout<<' ';
cout<<"filename"<<' '<<argv[i];
thefile.close();
}
}
}
}
}
答案 0 :(得分:2)
这里有很多问题。首先,您使用data Form :: * -> * where
Var :: Form s
Prim :: (Sat s r) => Pred s -> Marker r -> Form s
Simple :: (Sat s r) => Pred s -> Marker r -> Form s
Derived :: Form r -> Suffix r s -> Form s
Complex :: (Sat s r, Sat t P) =>
Form s -> Infix r -> Form t -> Form s
data FormA a where
FormA :: (Pred s -> Marker t -> a)
-> (Pred u -> Marker v -> a)
-> (a -> Suffix w x -> a)
-> (a -> y -> a -> a)
-> FormA a
foldForm :: FormA a -> Form s -> a
foldForm alg@(FormA prim _ _ _) (Prim p m) = prim p m
foldForm alg@(FormA _ simple _ _) (Simple p m) = simple p m
foldForm alg@(FormA _ _ derived _) (Derived f s) =
derived (foldForm alg f) s
foldForm alg@(FormA _ _ _ complex) (Complex f1 i f2) =
complex (foldForm alg f1) i (foldForm alg f2)
而不进行初始化。其次,您在读取第一个字符后关闭文件流。您可能希望在<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.sybase">
<resources>
<resource-root path="jconn3.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
块之后打印统计信息和文件名。适当的缩进将有助于使事情更清楚。