模拟“wc”命令 [英] simulation of a "wc" command

查看:63
本文介绍了模拟“wc”命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何人都可以帮我找到

档案中的总字数

我得到换行符,字符和blankspaces正确但计数

单词问题。我尝试了很多方法,但它没有帮助。

这是我的代码......

#include< string.h>

#include< stdio.h>


main(int argc,char * argv [])

{

FILE * fp;

int ch;

int chr = 0;

int totchr = 0;

int bspc = 0,totbspc = 0;

int nline = 0;

int word = 0, totwrd = 0;

int i = 0;


fp = fopen(argv [1]," r");

if(argc!= 2)

{

printf(" \tInsufficient arguments\\\
");

printf( " \ ttage:wrd< filename> \ n");

退出(0);

}

if(fp == NULL)

{

printf(文件打开错误\ n);

退出(0);

}

其他

{

while((ch = fgetc(fp))!= EOF)

{

if(ch =='''')

{

bspc ++;

}

if(ch ==''\ n'')

{

nline ++;

}

chr ++;

word ++;

}

}

// word = bspc + nline;

totchr + = chr;

totbspc + = bspc;

totwrd + = word;


printf(" \ nFile%s has\\\
",argv [1]);

printf(" \ n\twhite space:%d \ n",totbspc);

printf(" \twords是:%d \ n",totwrd);

printf(" \tcharacters是:%d \ n",totchr);

printf(" \tlines are:%d \ n \ nn",nline);


fclose(fp);

}

hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.
Here is my code......
#include <string.h>
#include <stdio.h>

main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;

fp=fopen(argv[1],"r");
if(argc != 2)
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <filename>\n");
exit(0);
}
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == '' '')
{
bspc++;
}
if(ch == ''\n'')
{
nline++;
}
chr++;
word++;
}
}
//word = bspc + nline;
totchr += chr;
totbspc += bspc;
totwrd += word;

printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);

fclose(fp);
}

推荐答案

2008-08-28, hi ********** @ gmail.com < hi **** ******@gmail.com写的:
On 2008-08-28, hi**********@gmail.com <hi**********@gmail.comwrote:

hi

任何人都可以帮我找到一个字的总数

文件

我正确地获取了换行符,字符和空格,但是计算了问题。我已经尝试了很多方法,但它没有帮助。

这是我的代码......


[代码剪辑]
hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.
Here is my code......

[code snipped]



你可以重新发布你的代码缩进(两个空格最简单吗?
在Usenet,IMHO上阅读),请?


另外,如果你不''需要使用格式说明符,puts()函数

会打印一个字符串,自动为它添加''\ n'',所以它会使

的东西更容易阅读。


-

Andrew Poelstra ap*******@wpsoftware.com

要给我发电子邮件,请使用以上电子邮件地址,.com设置为.net

Could you repost with your code indented (two spaces is easiest to
read on Usenet, IMHO) correctly, please?

Also, if you don''t need to use format specifiers, the puts() function
will print a string, automatically appending a ''\n'' to it, so it makes
things a bit easier to read.

--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net


hi ********** @ gmail.com 写道:

hi

任何人都可以帮我找到一个

文件中的单词总数

我正确地获得了换行符,字符和空格但是计算了

words ia problem.I尝试了很多方法,但它没有帮助。

这是我的代码......

#include< string.h>

#include< stdio.h>


main(int argc,char * argv [])

{

FILE * fp;

int ch;

int chr = 0;

int totchr = 0;

int bspc = 0,totbspc = 0;

int nline = 0;

int word = 0,totwrd = 0;

int i = 0;


fp = fopen(argv [1]," r");

if(argc!= 2)
hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.
Here is my code......
#include <string.h>
#include <stdio.h>

main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;

fp=fopen(argv[1],"r");
if(argc != 2)



argv [1]可能无法访问。这就是为什么你想要支票

argc。但在尝试访问argv [1]之前,您需要进行检查。

argv[1] may not be accessible. That''s why you want the check against
argc. But you need to do that check before you try to access argv[1].


{

printf(" \tInsufficient arguments \ n");

printf(" \ tusage:wrd< filename> \ n");

exit(0);

}

if(fp == NULL)

{

printf("文件打开错误\\ n");

退出(0);

}

其他

{

while((ch = fgetc(fp))!= EOF)

{

if(ch =='''')

$

bspc ++;

}

if(ch ==''\ n'')

{

nline ++;

}

chr ++;

word ++;


}

}

// word = bspc + nline;

totchr + = chr;

totbspc + = bspc;

totwrd + = word;


printf(" \ nFile%s has\\\
",argv [1]) ;

printf(&\\; \ n\twhite空格是:%d \ n&q uot;,totbspc);

printf(" \twords是:%d \ n",totwrd);

printf(" \tcharacters是: %d \ n",totchr);

printf(" \tlines are:%d \ n \ n",nline);


fclose(fp);

}
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <filename>\n");
exit(0);
}
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == '' '')
{
bspc++;
}
if(ch == ''\n'')
{
nline++;
}
chr++;
word++;
}
}
//word = bspc + nline;
totchr += chr;
totbspc += bspc;
totwrd += word;

printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);

fclose(fp);
}



这与你对单词的定义有关。和白色空间。 unix

实用程序wc将一个单词称为由

空格分隔的字符串。如果这是你试图模仿的行为,那么

totbspc可能是你文件中单词数量的一个很好的近似值。


fgetc返回EOF后进行一些错误检查,以确定最后是否需要发生
文件结束事件或发生错误。


-

Pietro Cerutti

It''s most about your definition of "words" and "white spaces". The unix
utility wc refers to a word as a string of characters delimited by a
blank space. If that''s the behavior that you''re trying to mimic, then
totbspc may be a good approximation of the number of words in your file.

Some error checking after fgetc returns EOF to see whether an
end-of-file event or an error occurred may be needed at the end.

--
Pietro Cerutti


8月27日,10:18 * pm,hiteshtha ... @ gmail.com写道:
On Aug 27, 10:18*pm, hiteshtha...@gmail.com wrote:

hi

任何人都可以帮我找到

档案中的总字数

我正确地获得了换行符,字符和空格,但是计算了问题。我已经尝试了很多方法,但它没有帮助。
hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.



定义问题。请明确点。你的意见是什么?你的

输出是多少?你想要什么输出?

Define problem. Be specific. What was your input? What was your
output? What output do you want?


这是我的代码......

#include< string.h>

#include< stdio.h>


main(int argc,char * argv [])

{

FILE * fp;

int ch;

int chr = 0;

int totchr = 0;

int bspc = 0,totbspc = 0;

int nline = 0;

int word = 0,totwrd = 0;

int i = 0;


fp = fopen(argv [1]," r");

if(argc!= 2)

{

printf(" \tInsufficient arguments \ n");

printf(" \ tusage:wrd< filename> \\ \\ n");

退出(0);}
Here is my code......
#include <string.h>
#include <stdio.h>

main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;

fp=fopen(argv[1],"r");
if(argc != 2)
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <filename>\n");
exit(0);}



显然这个测试应该在调用fopen之前进行。


如果失败,为什么返回零。使用stdlib.h中的EXIT_FAILURE。

Obviously this test should come before the call to fopen.

Why return zero if it failed. Use EXIT_FAILURE from stdlib.h.


>

if(fp == NULL)

{

printf(文件打开时出错\ n);

退出(0);}


else

{

while((ch = fgetc(fp))!= EOF)

{

if(ch = ='''')

{

bspc ++;}


if(ch ==''\ n''' )

{

nline ++;}


chr ++;

word ++;
>
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);}

else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == '' '')
{
bspc++;}

if(ch == ''\n'')
{
nline++;}

chr++;
word++;



为什么要为每个字符递增单词。你应该

只有在当前字符为空格时才增加它(参见你的参考中的isspace

)。

Why are you incrementing word for every character. You should
increment it only if the current character is white space (see isspace
in your reference).


>

}

}
>
}
}



一致的缩进样式将为您节省大量时间你的

编程工作。

A consistent indenting style will save you a lot of time in your
programming efforts.


>

// word = bspc + nline;

totchr + = chr;

totbspc + = bspc;

totwrd + = word;
>
//word = bspc + nline;
totchr += chr;
totbspc += bspc;
totwrd += word;



左侧操作数是否为非零?

Are the left side operands ever non-zero?


>

printf(" \ nFile%s has\\\
",argv [1]);

printf(" \ n\twhite space is:%d \ n" ,totbspc);

printf(" \twords是:%d \ n",totwrd);

printf(" \tcharacters are:%d \ n",totchr);

printf(" \tlines are:%d \ n \ nn",nline);


fclose(fp);


}
>
printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);

fclose(fp);

}


这篇关于模拟“wc”命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆