LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1330|回复: 17

一个C的问题,请C熟悉的多多指教

[复制链接]
发表于 2005-8-29 17:51:09 | 显示全部楼层 |阅读模式
[PHP]     #include <stdio.h>
      int argc;
      char *argv[2];
      int main(argc,argv)
      {
              FILE *fp;
              int i;
              fp=fopen(argv[1],"rb");
              if(fp==NULL)
                      puts("File open error");
              while(!feof(fp))
                      putchar(fgetc(fp));
     
              printf("\n");
              i=fclose(fp);
              if(i==0)
                      printf("O,K");
              else
                      puts("File close error");
     
      }[/PHP]

其中有错误,subscripted value is neither array nor pointer
,说fopen(argv[1],"rb)这里的argv[1]不是数组也不是指针。

其实也就是要让这个程序能接收参数   gcc -o file ,能file file.c就把才的内容显示出来了。
发表于 2005-8-29 18:00:40 | 显示全部楼层
脚本语言用多了吧? 哪有你这样写 c 程序的,
建议:找本c语言的书看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-8-29 18:13:33 | 显示全部楼层
请帮我纠正一些错误,上面的程序我是参照网上C教程中copy的。http://www.ddvip.net/program/c/index1/132.htm


[PHP]#include <stdio.h>

int main()
{
          FILE *fp;
          char filename[20];
          pfile(fp,filename);
  }

  int pfile(FILE *fp,char filename[])
  {

          fp=fopen(gets(filename),"rb");
          while(!feof(fp))
                  putchar(fgetc(fp));
}[/PHP]

我用在运行之后输入参数的方式,测试,倒是可以,程序存在的许多问题,现在还菜,多多指出。
编译的时候提示了gets is dangerous。
回复 支持 反对

使用道具 举报

发表于 2005-8-29 18:37:50 | 显示全部楼层
你再仔细看一下下面三行的顺序:
  1.       int argc;
  2.       char *argv[2];
  3.       int main(argc,argv)
复制代码

另外,现在很少有人用这种风格,当然如果你用了,证明你很雅
回复 支持 反对

使用道具 举报

发表于 2005-8-29 18:52:12 | 显示全部楼层
Post by mantou

编译的时候提示了gets is dangerous。

man gets:


       Never use gets().  Because it is impossible to tell without knowing the
       data in advance how many  characters  gets()  will  read,  and  because
       gets() will continue to store characters past the end of the buffer, it
       is extremely dangerous to use.  It has  been  used  to  break  computer
       security.  Use fgets() instead.
回复 支持 反对

使用道具 举报

发表于 2005-8-29 19:48:51 | 显示全部楼层
int argc;
      char *argv[2];
      int main(argc,argv)
我也还是头一次看到这么写C代码的
回复 支持 反对

使用道具 举报

发表于 2005-8-29 20:07:51 | 显示全部楼层
这是一种标准写法
平时大家都简略了
回复 支持 反对

使用道具 举报

发表于 2005-8-29 20:14:54 | 显示全部楼层
Post by orphen
这是一种标准写法
平时大家都简略了

真是嘛?
回复 支持 反对

使用道具 举报

发表于 2005-8-29 20:23:18 | 显示全部楼层
No!!!!! This is not a standard.

ANSI:
int main(int argc, char *argv[])
{
...
}

K&R:   (old-fashioned and rarely used)
int main(argc, argv)
int argc;
char *argv[];
{
...
}

just like K&R and very stupid.
int argc;
char *argv[2];
int main(argc,argv)
回复 支持 反对

使用道具 举报

发表于 2005-8-29 20:31:08 | 显示全部楼层
and other damned stupid forms:

int argc;
int main(argc, argv)
{
...
}
char *argv[2];

and

int main(argc, argv)
{
...
}
int argc;
char *argv[2];

All these SUCK hard.....
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表