LinuxSir.cn,穿越时空的Linuxsir!

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

缺少gets函数,怎么办?

[复制链接]
发表于 2003-10-10 23:45:05 | 显示全部楼层 |阅读模式
[root@localhost c]# gcc match_char.c
/tmp/ccONSwnZ.o: In function `main':
/tmp/ccONSwnZ.o(.text+0x1b): the `gets' function is dangerous and should not be used.
[root@localhost c]# cat match_char.c
#include <stdio.h>
#include <string.h>
main()
{       char a[80],b[40];
        int na,nb,i,j,flag;
        gets(a);
        gets(b);
        na=strlen(a),nb=strlen(b);
        flag = -1;
        for (i=0;na-i >= nb;i++)
        {
                flag = -2;
                for (j=0;j<nb;j++)
                        if (a[i+j] != b[j])
                        { flag=-1;break;}
                if(flag == -2)
                { flag=i+1;break;}
        }
        printf ("%s\n%s\n%d\n",a,b,flag);
}
[root@localhost c]#
发表于 2003-10-10 23:47:36 | 显示全部楼层
应该编译号了吧。那值时个警告
 楼主| 发表于 2003-10-11 00:27:57 | 显示全部楼层
不懂:ask :ask 请说清楚些。
发表于 2003-10-11 00:33:26 | 显示全部楼层
你看看目录下有没有a.out。
用GETS函数,GCC编译的时候给出警告,但不终止编译。
也就是说你一经编译号了。
 楼主| 发表于 2003-10-11 00:41:54 | 显示全部楼层
有。但我改了名还是那样?
[root@localhost c]# ls
a_b_toA.c  array1.c       break.c         for.c           match_char.c
a_c.c      array_char     continue.c      gcc_version_rh  pipc.c
a_count.c  array_char.c   count_to_100.c  goto.c          switch_case.c
a.out      array_input.c  do_while.c      if.c            t
array0.c   book           f.c             learn0.c        while.c
[root@localhost c]# gcc -o match_char match_char.c
/tmp/ccBtRWji.o: In function `main':
/tmp/ccBtRWji.o(.text+0x1b): the `gets' function is dangerous and should not be
used.
[root@localhost c]# gcc -o f f.c
[root@localhost c]#
发表于 2003-10-11 00:47:04 | 显示全部楼层
gets比较危险(缓冲区溢出),改用其他函数,比如fgets,不过fgets不会去除结尾的换行符,记得用buf[strlen(buf) - 1] = '\0';

象你写的gcc -o match_char match_char.c
虽然有警告,但是match_char已经编译好了,你运行一下看看
 楼主| 发表于 2003-10-11 11:37:08 | 显示全部楼层
[root@localhost c]# ./match_char
asdfghj
dj
asdfghj
dj
-1
[root@localhost c]# ./match_char
aasdfgh
sd
aasdfgh
sd
3
[root@localhost c]#

[root@localhost c]# ./match_char
as
asfg
as
asfg
-1
[root@localhost c]#
其实这个程序我是看不懂。
书上说 break是跳出循环.
if(flag == -2)
{ flag=i+1;break;}

for (j=0;j<nb;j++)
if (a[i+j] != b[j])
{ flag=-1;break;}
分别是跳出到哪里?特别是上面那个?
你给的函数不会用buf是什么意思?
发表于 2003-10-11 16:38:10 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <string.h>

  3. main()
  4. {
  5.   char a[80], b[40];
  6.   int na, nb, i, j, flag;

  7.   gets(a);
  8.   gets(b);
  9.   na=strlen(a);
  10.   nb=strlen(b);
  11.   flag = -1;
  12.   for(i = 0; na - i >= nb; i++){
  13.     flag = -2;
  14.     for(j = 0; j < nb; j++)
  15.       if (a[i+j] != b[j]){
  16.         flag = -1;
  17.         break; [color=red]/* 跳出for(j = 0; j < nb; j++) */[/color]
  18.       }
  19.     if(flag == -2){
  20.       flag = i + 1;
  21.       break; [color=red]/* 跳出for(i = 0; na - i >= nb; i++){ */[/color]
  22.     }
  23.   }
  24.   printf("%s\n%s\n%d\n", a, b, flag);
  25. }
复制代码


“你给的函数不会用buf是什么意思?”
这个没看懂,什么不会用buf?
 楼主| 发表于 2003-10-11 19:47:34 | 显示全部楼层
buf[strlen(buf) - 1] = '\0'

基础差,这里的buf是什么意思?
发表于 2003-10-11 21:00:46 | 显示全部楼层
buf是接收输入的缓冲区,比如
char buf[80];

fgets(buf, 79, stdin);
buf[strlen(buf) - 1] = '\0';

比如你输入"12345",然后回车,程序实际接收到的输入是"12345\n",用gets的时候gets会去除结尾的'\n',而fgets不会去除结尾的'\n',
buf[strlen(buf) - 1] = '\0';
的作用是把字符串的最后一个字符('\n')去掉。

相应的,puts输出的时候会在输出的字符串结尾加上'\n',fputs就不会。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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