gcc 里不能使用gets() 和 puts() 函数的解决办法

来源:互联网 发布:儿童电动牙刷价格淘宝 编辑:程序博客网 时间:2024/06/07 11:45

测试代码,删除字符串空格符:

#include<stdio.h>int main(void) {    char a[100];    int i = 0, j = 0;    printf("input:");    fgets(a, 100, stdin);  // gcc中 gets puts函数不能用!! stdin 键盘输入.    for (i = 0; * (a + i) != '\0'; i++) {        if (*(a + i) != ' ')            a[j++] = a[i] ;    }    a[j] = '\0';    //这里一定要记得加上'\0'结束符,j在最后    //一次执行的时候已经自加1了,所以这里不是a[j+1]='\0'.    printf("output:");    fputs(a, stdout);    return 0;}

运行:

input:ashf s    sjdhf    sdfs lj loutput:ashfssjdhfsdfsljl请按任意键继续. . .

fgets()fputs()函数代替gets()puts()函数。

0 0
原创粉丝点击