读书人

哪位高手能告诉小弟我错那里了

发布时间: 2012-02-22 19:36:54 作者: rapoo

谁能告诉我错那里了?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GET_ARRAY_LEN(arry,len_)={sizeof(array)/sizeof(array[0])
#define IN 1;
#define OUT 0;
FILE *fin;
int main(int argc, char * argv[]) {
char c;
if((fin=fopen(argv[1],"r"))==NULL){
printf("can't open the file $s\n",argv[1]);
exit(-1);
}
int nl,nw,nc,state;
while((c=fgetc(fin))!=EOF){
nc++;
if(c==' '||c=='\n'||c=='\t')
state=OUT ;
else if( state == OUT){
state=IN;
++nw;
}
}

[解决办法]
这里不用分号
#define IN 1
#define OUT 0

[解决办法]
GET_ARRAY_LEN好像也有问题,这样可能好一点
#define GET_ARRAY_LEN(arry,len_) len_=sizeof(arry)/sizeof(arry[0])
不过我个人不喜欢这样用宏定义,不如用函数更安全。

[解决办法]
还有
使用argv[1]之前,一定要先判断argc是否大于1
printf里面的%s打错了

[解决办法]
int nl,nw,nc,state; 没有赋初值
[解决办法]

C/C++ code
#include  <stdio.h> #include  <stdlib.h> #include  <string.h> #define GET_ARRAY_LEN(arry,len_) sizeof(array)/sizeof(array[0]) #define IN    1#define OUT   0FILE   *fin = NULL; int main(int argc, char * argv[]){     char c;     if(argv[1] != '\0')    {        if( (fin=fopen(argv[1],"r") ) == NULL)        {             printf("can't open the file %s\n",argv[1]);             exit(-1);         }         int nw = 0;        int nc = 0;        int state;         while((c=fgetc(fin)) != EOF)        {             nc++;                         if( (c == ' ') || (c == '\n') || (c == '\t') )             {                state=OUT;             }            else if( state == OUT)            {                  state=IN;                 ++nw;             }         }        fclose(fin);    }    return 0;} 

读书人网 >C语言

热点推荐