文件打开与关闭C文件操作用库函数实现,包含在stdio.h中。 系统自动打开和关闭三个标准文件: 文件读写操作当我们把文件打开之后,就可以对它进行读与写的操作。 #include <stdio.h>int main(void){FILE *fp = NULL;char ch;fopen_s(&fp, "D:\\text.txt", "w");if (fp == NULL){exit(0);}else{ch = getchar();while (ch != '#'){fputc(ch, fp); //字符被写入filename表示的文件中putchar(ch); //字符被输出到显示器ch = getchar();}putchar(10); // 向屏幕输出一个换行符fclose(fp);} return 0;} 例如:将一个磁盘文件中的信息复制到另一个磁盘文件中。 #include <stdio.h>#include <stdlib.h>int main(void){FILE *in, *out;char ch;if ((fopen_s(&in,"D:\\dd.txt", "r")) != 0){printf("Cannot open infile.\n"); exit(0);}if ((fopen_s(&out,"D:\\ee.txt", "w")) != 0){printf("Cannot open outfile.\n"); exit(0);}while (!feof(in))fputc(fgetc(in), out);fclose(in); fclose(out);return 0;} 读/写文件中的字符串数据块输入输出函数:fread与fwrite 案例: #include <stdio.h>#define SIZE 4struct student_type{char name[10];int num;int age;char addr[15];}stud[SIZE];void display();void save();int main(){int i;for (i = 0; i < SIZE; i++){scanf_s("%s", &stud[i].name, 10);scanf_s("%d", &stud[i].num);scanf_s("%d", &stud[i].age);scanf_s("%s", &stud[i].addr, 15);}save();display();return 0;}void save(){ FILE *fp; int i; if ((fopen_s(&fp,"D:\\stu_list", "wb")) != 0) {printf("cannot open file\n");return; } for (i = 0; i<SIZE; i++)if (fwrite(&stud[i], sizeof(struct student_type), 1, fp) != 1) printf("File write error\n");fclose(fp);}void display(){ FILE *fp; int i; if ((fopen_s(&fp,"D:\\stu_list", "rb")) != 0) {printf("Cannot open file\n"); return; } for (i = 0; i<SIZE; i++) {fread(&stud[i], sizeof(struct student_type), 1, fp);printf("%-10s %4d %4d %-15s\n", stud[i].name,stud[i].num, stud[i].age, stud[i].addr); }fclose(fp);} 格式化读写文件操作fscanf_s() 和 fprintf() 的读写对象不是键盘和显示器,而是磁盘数据文件。 文件格式化输入函数 fscanf_s的函数原型为: 所在头文件:<stdio.h> 返回值: 故一般可根据该函数的返回值是否为 EOF 来判断是否已读到文件结尾处。 案例: #include<stdio.h>int main(){int ixnumber, iynumber;FILE *fp=NULL;fopen_s(&fp, "D:\\datafile.txt", "r"); if (fp == NULL){printf("Failed to open the File!\n");exit(0);}fscanf_s(fp, "%d,%d", &ixnumber, &iynumber); //从fp所指文件中读取一个整数保存到变量ixnumber, iynumberprintf("\n%d,%d\n\n", ixnumber, iynumber);fclose(fp);return 0;} 结果为: 文件格式化输出函数 fprintf 的函数原型为: 【实战案例】格式化读写文件操作(二) #include<stdio.h>int main(){int ixnumber, iynumber;FILE *fp=NULL;fopen_s(&fp, "D:\\datafile.txt", "w"); if (fp == NULL){printf("Failed to open the File!\n");exit(0);}char name[10] = "张三";char no[15] = "20190410001";int age = 17;fprintf(fp, "%s\t%s\t%d\n", name, no, age);fclose(fp);return 0;} 文件定位函数
作业使用案例(自己可以全部完成一遍):使用fgetc()、fputc()、fgets()、fputs()、fread()、fwrite()、fscanf_s()、fprintf()、rewind()、fseek()实现文件输入输出操作。 代码如下: #include<stdio.h>#include<stdint.h>#include<stdlib.h>#include<Windows.h>int main(){FILE *fp = NULL;fopen_s(&fp, "text.txt", "r");if (fp == NULL){printf("文件打开失败\n");}else{printf("文件打开成功\n");}char ch;ch = fgetc(fp);// 读取字符while (ch != EOF){putchar(ch);// 打印读取的字符ch = fgetc(fp);// 再一次读 重新赋值ch}printf("\n");fclose(fp);fopen_s(&fp, "text.txt", "a+");char ch1;ch1 = getchar();while (ch1 != '#'){fputc(ch1, fp);ch1 = getchar();}fclose(fp);FILE *in, *out;char ch2;if ((fopen_s(&in, "dd.txt", "r")) != 0){printf("文件打开失败!\n");}if ((fopen_s(&out, "ee.txt", "w")) != 0){printf("文件打开失败!\n");}while (!feof(in)){fputc(fgetc(in), out);}fclose(in);fclose(out);FILE*fp2;fopen_s(&fp2, "bb.txt", "a+");char str[] = "chuan";fputs(str, fp2);fclose(fp2);FILE *fp1;int a, b;fopen_s(&fp1, "file.txt", "a+");if (fp1 == NULL){printf("文件打开失败\n");}fscanf_s(fp1,"%d,%d", &a, &b);printf("%d,%d\n", a, b);fprintf(fp1, "%d%d\n", a, b);fclose(fp1);FILE*fq3;fopen_s(&fq3,"bb.txt", "wb+");int se = 1;if (fread(&se, sizeof(4), 1, fq3) != 0){printf("文件打开失败");}if (fwrite(&se, sizeof(4), 1, fq3) != 0){printf("文件写入失败");}system("pause");return 0;} 结果如下: |
|