提示用户输入信息,并将用户输入的写入一个文件,当用户输入的信息 是quit的时候,打印输出用户输入的总长度

/***************************************************
> Copyright (C) 2018 ==yangzheng== All rights reserved.
> File Name: shuru.c
# Author: feiren
# mail:[email protected]
> Created Time: 2018年01月15日 星期一 09时59分50秒
***************************************************/
/***************************************************
提示用户输入信息,并将用户输入的写入一个文件,当用户输入的信息
是quit的时候,打印输出用户输入的总长度
****************************************************/


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>


int main(int argc,const char* argv[])
{
FILE *fp = fopen("./01.txt","w");   // 打开文件,只读
char buff[128]={0}; 
int count = 0;
while(1){
  fgets(buff,sizeof(buff),stdin);
//printf("|%s|%c\n",buff,buff[strlen(buff)-1]);  // 用于验证fgets是否能够能收回车和buff是否读一行后被清空
if(strcmp(buff,"quit\n")==0)
break;
count+=strlen(buff);
fputs(buff,fp);
}
printf("len:%d\n",count);//
fclose(fp);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/flying_man_/article/details/79063411