用法比较单一,请大家结合代码和注释进行理解
#include "stdio.h"
#include "stdlib.h"
int main() {FILE *fp; //定义文件指针char filename[200]; //存放输入的文件名字char str[200]; //存放一个职工的信息int i, j;printf("Please enter the file name:\n");gets(filename); //输入文件名字fp = fopen(filename, "w"); //以文本模式按只写方式打开文件if (fp == NULL) //判断文件是否成功打开{printf("File open failed!\n");exit(0);}printf("Please input the information of 5 employees:\n");for(i=0;i<5;i++) //输入五个职工的信息{gets(str);for(j=0;str[j]!='\0';j++){ fputc(str[j],fp); //将str里面的内容写入fp文件中}fputc('\n',fp); }fclose(fp); //关闭文件printf("The employee information was successfully saved in the %s file\n",filename);return 0;}
测试结果
公众号:风景邮递Yuan 拿免费U校园答案