qq_43412960 2019-10-09 13:27 采纳率: 76.5%
浏览 458
已采纳

C语言 gets函数相关问题

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<String.h>
int main()
{
FILE *fp;
char ch;

fp=fopen("F:\\啦啦.txt","at+");  
    /* if((fp=fopen("F:\\啦啦.txt","at+"))==NULL)
      {
            printf("Cannot open file strike any key exit!");
            getch();
            exit(1);
      }
     ch=fgetc(fp);
    while (ch != EOF)
      {
      putchar(ch);
      ch=fgetc(fp);
      } *///这个过程是进行读数据的测试过程
     //接下来进行语法分析

ch = getchar();  //这里回车作为输入结束
 FILE *stream = stdin;

 int stdin_len = strlen(stream->_ptr);
 stream->_ptr[strlen(stream->_ptr)-2]='\0';
 if (stdin_len > 0)
 {
    fputs(stream->_ptr-1, fp);    //stream->_ptr 后面会带有\n\n  不需要就开空间复制前面的输入字符串
 }

 char  en[20];
     gets(en);
 printf("%s",en);//测试gets函数效果
system("pause");
return 0;

}

我想测试下gets函数效果,这个函数就是运行的时候 ,等待用户输入,然后我弄了个输出,输出结果很变态图片说明

输出结果 变少了 为什么呢。

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-10-09 14:19
    关注

    int stdin_len = strlen(stream->_ptr);
    stream->_ptr[strlen(stream->_ptr)-2]='\0';
    调试下这里,感觉是越界了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?