C语言:
#include<stdio.h>
#include<stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
if((fp = fopen("words","a+"))==NULL)
{
fprintf(fp,"can not open\"word\"file.\n");
exit(1);
}
puts("enter words to add to the file press the enter");
puts("key at the beginning of a line to terminate ");
while(gets(words)!=NULL&&words[0]!='\0')
fprintf(stdout,"%s",words);//如果把这里的stdout改为fp或者是stderr等其他文件指针有什么区别呢?具体讲讲fprintf三个参数的意义
puts("File contents:");
rewind(fp);
while(fscanf(fp,"%s",words)==1)
puts(words);
if(fclose(fp)!=0)
fprintf(stderr,"error closing file\n");
return 0;
}