前面写过一篇fgets()从键盘获取数据然后输出到屏幕上,现在用fgets()来将从键盘获得数据然后写入到文件当中,先贴代码。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MaxSize 20
#define true 1
void main()
{
char *proverbs[]=
{
"Many a mickle makes a muckle.\n",
"Too many cooks spoil the broth.\n",
"He who laughs last didn't get the joke in the first place.\n"
};
int count=sizeof(proverbs)/sizeof(proverbs[0]);
int i=0;
int c=1;
char buffer[MaxSize];
char *fpath="E:\\C++测试\\C语言学习\\C语言学习\\files\\file.txt";
FILE *pfile=NULL;
if(!(pfile=fopen(fpath,"w")))
{
printf("Error opening\n");
exit(1);
}
for(i=0;i<count;i++)
{
fputs(proverbs[i],pfile);
}
while(true)
{
printf("请输入第 %d 句: ",c);
fgets(buffer,sizeof(buffer),stdin);
//printf("\n第%d句输入的字符数: %d \n",strlen(buffer));
c++;
if(buffer[0]=