允许程序有两个可选变元--编译出错

liushac 2009-09-25 11:00:57
/***********************************************************************
Copyright (c) 2009,liushac
All rights no reserved.

Name: <<C程序设计语言>>例程5-10
ID: hello.c
功能: 允许程序有两个可选变元。其中一个表示“与模式串不匹配的文本行”,
另一个表示“每个被打印文本行前面都有相应的行号”。
-x(x代表except)表示打印所有与模式串不匹配的文本行;
-n(n代表number)表示打印行号。

Version: 0.1
Author: c.
Date: Sep 25, 2009

***********************************************************************/
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int getline(char *line,int max);

main(int agrc,char *argv[])
{
char *line;

long lineno=0;
int c,except=0,number=0,found=0;

while(--argc>0 && (* ++argv)[0]=='-')
{
while(c=*++argv[0])
switch(0)
{
case 'x':
except=1;
break;
case 'n':
number=1;
break;
default:
printf("find: illegal option %c\n",c);
argc=0;
found=-1;
break;
}
if (argc !=1)
printf ("Usage: find -x -n pattern\n");
else
while(getline(*line,MAXLINE)>0)
{
lineno++;
if ((strstr(*line,*argv)!=NULL)!=except)
{
if(number)
printf("%d:",lineno);
printf("%s",line);
found++;
}
}
return found;
}
}
/* getline: 将一行读入s中并返回其长度 */
int getline(char *s, int lim)
{
char *p;
int c;

p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (int)(p - s);
}


出错提示:
--------------------Configuration: hello - Win32 Debug--------------------
Compiling...
hello.c
D:\Dev-Cpp\hello.c(14) : error C2065: 'argc' : undeclared identifier
D:\Dev-Cpp\hello.c(34) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
D:\Dev-Cpp\hello.c(34) : warning C4024: 'getline' : different types for formal and actual parameter 1
D:\Dev-Cpp\hello.c(37) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char '
D:\Dev-Cpp\hello.c(37) : warning C4024: 'strstr' : different types for formal and actual parameter 1
执行 cl.exe 时出错.

hello.obj - 1 error(s), 0 warning(s)
...全文
156 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2009-09-25
  • 打赏
  • 举报
回复
指针……

/***********************************************************************
Copyright (c) 2009,liushac
All rights no reserved.

Name: <<C程序设计语言>>例程5-10
ID: hello.c
功能: 允许程序有两个可选变元。其中一个表示“与模式串不匹配的文本行”,
另一个表示“每个被打印文本行前面都有相应的行号”。
-x(x代表except)表示打印所有与模式串不匹配的文本行;
-n(n代表number)表示打印行号。

Version: 0.1
Author: c.
Date: Sep 25, 2009

***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1000

int getline(char *line,int max);

main(int agrc,char *argv[])
{
char *line=(char *)malloc(512);

long lineno=0;
int c,except=0,number=0,found=0;

while(--agrc>0 && (* ++argv)[0]=='-')
{
while(c=*++argv[0])
switch(c)
{
case 'x':
except=1;
break;
case 'n':
number=1;
break;
default:
printf("find: illegal option %c\n",c);
agrc=0;
found=-1;
break;
}
if (agrc !=1)
printf ("Usage: find -x -n pattern\n");
else
while(getline(line,MAXLINE)>0)
{
lineno++;
if ((strstr(line,*argv)!=NULL)!=except)
{
if(number)
printf("%ld:",lineno);
printf("%s",line);
found++;
}
}
free(line);
return found;
}
}
/* getline: 将一行读入s中并返回其长度 */
int getline(char *s, int lim)
{
char *p;
int c;

p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (int)(p - s);
}
forster 2009-09-25
  • 打赏
  • 举报
回复
。。。。
liushac 2009-09-25
  • 打赏
  • 举报
回复
用数组会写,但是这里用指针个人有点困难
liushac 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 brookmill 的回复:]
main那一行的argc拼写错误,改过来就可以编译通过了。
[/Quote]
[Quote=引用 3 楼 wanjingwei 的回复:]
你的argc都拼写错了,而且strstr函数和getline函数里有个参数类型不正确
[/Quote]
argc改过来可以编译了,strstr函数参数类型不会改,对指针还是一知半解
csdn怎么老跳出登陆界面?!
brookmill 2009-09-25
  • 打赏
  • 举报
回复
好像应该把line的定义改成这样比较好
char line[100];
brookmill 2009-09-25
  • 打赏
  • 举报
回复
6楼错了

getline和strstr用的都不对,应该是
getline(line,MAXLINE)
strstr(line,*argv)
而且line好像没初始化
brookmill 2009-09-25
  • 打赏
  • 举报
回复
getline和strstr用的都不对,应该是
getline(*line,MAXLINE)
(strstr(*line,*argv)
而且line好像没初始化
ysuliu 2009-09-25
  • 打赏
  • 举报
回复
这个,太诡异了,看了半天,原来是这个问题~~
rlxtime 2009-09-25
  • 打赏
  • 举报
回复
同上
wanjingwei 2009-09-25
  • 打赏
  • 举报
回复
你的argc都拼写错了,而且strstr函数和getline函数里有个参数类型不正确
brookmill 2009-09-25
  • 打赏
  • 举报
回复
main那一行的argc拼写错误,改过来就可以编译通过了。
brookmill 2009-09-25
  • 打赏
  • 举报
回复
main(int agrc,char *argv[])
liushac 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 brookmill 的回复:]
12楼的代码多了一对大括号,删掉就好了
C/C++ codewhile(--agrc>0&& (*++argv)[0]=='-')
{// 删掉这个while(c=*++argv[0])
......
free(line);return found;
}// 删掉这个}
[/Quote]
多谢brookmill,经测试正常了,结贴了
brookmill 2009-09-25
  • 打赏
  • 举报
回复
12楼的代码多了一对大括号,删掉就好了
    while(--agrc>0 && (* ++argv)[0]=='-')
{ // 删掉这个
while(c=*++argv[0])
......
free(line);
return found;
} // 删掉这个
}
liushac 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 mstlq 的回复:]
指针……

[/Quote]

感谢mstlq,
char *line=(char *)malloc(512);
free(line);
不过上面#12楼的代码运行有点似乎有点问题,用hello.exe -x -n findme命令后退出了,正常应该等候用户输入文本行,下面用数组没有问题




#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int getline(char line[],int max);

main(int argc,char *argv[])
{
char line[MAXLINE];

long lineno=0;
int c,except=0,number=0,found=0;

while(--argc>0 && (* ++argv)[0]=='-')
while(c=*++argv[0])
switch(c)
{
case 'x':
except=1;
break;
case 'n':
number=1;
break;
default:
printf("find: illegal option %c\n",c);
argc=0;
found=-1;
break;
};
if (argc !=1)
printf ("Usage: hello -x -n pattern\n");
else
while(getline(line,MAXLINE)>0)
{
lineno++;
if ((strstr(line,*argv)!=NULL)!=except)
{
if(number)
printf("%d:",lineno);
printf("%s",line);
found++;
}
}
return found;
}
/* getline:将一行读入s中并返回其长度 */
int getline (char s[],int maxlim)
{
int c;
int i;
i=0;

while (i<(maxlim-1) && (c=getchar())!=EOF && c!='\n'){
s[i]=c;
++i;
}
if (c=='\n'){
s[i]=c;
++i;
}
s[i]='\0';
return i;
}

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧