fgets学习

#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
int main()
{
    char s[50]="";
    FILE *p=fopen("c.txt","w");
    while(1)
    {
        memset(s,0,sizeof(s));
        fgets(s,50,stdin);
        if(s[strlen(s)-1]=='\n')
            s[strlen(s)-1]='\0';
        if(strcmp(s,"exit")==0)
          break;
        fputs(s,p);
    }
    fclose(p);
    printf("end\n");
    return 0;
}

使用fgets循环读取键盘输入,删除换行符,使用exit字符串退出

猜你喜欢

转载自blog.csdn.net/cp_srd/article/details/83272725