ACM金牌带你零基础直达C语言精通-课程资料
本笔记属于船说系列课程之一,课程链接:ACM金牌带你零基础直达C语言精通
https://www.bilibili.com/cheese/play/ep159068?csource=private_space_class_null&spm_id_from=333.999.0.0
你也可以选择购买『船说系列课程-年度会员』产品『船票』,畅享一年内无限制学习已上线的所有船说系列课程:船票购买入口
https://www.bilibili.com/cheese/pages/packageCourseDetail?productId=598
做题网站OJ:HZOJ - Online Judge
Leetcode :力扣 (LeetCode) 全球极客挚爱的技术成长平台
一.文件类型
现在假如有一个文件data.txt和一个可执行程序:
在文件中如何存储文件类型:FILE *,FILE指针类型,然后我们可以通过定义一个FILE指针类型定义一个指针变量进行指向我们需要进行操作的文件。
现在fp是没有指向data.txt文件的,现在需要借用一个函数才能进行指向data.txt;
这个函数是fopen
现在又了指向data.txt的文件指针了,如果需要对data.txt文件中输入内容,需要使用函数fprintf
程序测试:
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("./data.txt", "w"); //当没有成功打开文件时,打印提示信息,并结束程序 if (fp == NULL) { printf("failed to open file\n"); exit(1); } //向文件中打印内容 fprintf(fp, "hello world\n"); int a = 123, b = 456; fprintf(fp, "a = %d, b = %d\n", a, b); fclose(fp); return 0; }
执行结果:
打开data.txt文件查看:
文件打开模式:
二进制模式(在任何模式后加上"b"):
- 以二进制模式打开文件,用于处理二进制文件。例如,"rb" 表示以只读模式打开二进制文件。
对于r和r+,w和w+的代码演示:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> void r_access() { //以只读方式打开data.txt FILE *fp = fopen("./data.txt", "r"); if (fp == NULL) { printf("r : failed to open file\n"); exit(1); } char s[100]; //读取文件中的内容 fscanf(fp, "%[^\n]", s); printf("r : s = %s\n", s); fclose(fp); fp = fopen("temp.txt", "r"); //如果以r模式打开不存在的文件,那么会打开失败返回NULL空地址 if (fp == NULL) { printf("r : open erroe\n"); } //使用过后记得关闭 return ; } void rand_file_name(char *file_name, int n) { for (int i = 0; i < n; i++) { file_name[i] = rand() % 26 + 'a'; } file_name[n] = 0; //在文件名末尾加上.txt //strcat作用将第一个参数的后面加上第二个参数的字符串 strcat(file_name, ".txt"); return ; } void w_access() { //测试存在的文件 FILE *fp = fopen("data.txt", "w"); if (fp == NULL) { printf("w : failed to open file\n"); exit(1); } //向文件中输入内容,并且在输入前会清空文件中的内容 fprintf(fp, "w :hahahahah, world\n"); fprintf(fp, "hello nihao\n"); fclose(fp); char file_name[100] = {0}; rand_file_name(file_name, 10); //当用w打开不存在的文件时,会创建新的文件,并且文件名就是fopen的第一个参数 printf("w : open new name : %s\n", file_name); fp = fopen(file_name, "w"); fclose(fp); return ; } void r_plus_access() { //现在打开data.txt可以对文件进行读取和写入内容 FILE *fp = fopen("data.txt", "r+"); if (fp == NULL) { printf("r+ : failed to open file\n"); exit(1); } char s[100]; fscanf(fp, "%s", s); printf("r+ : s = %s\n", s); //在r+模式下写入内容,默认会在文件末尾进行写入 fprintf(fp, "000000"); fclose(fp); //打开不存在的文件 fp = fopen("data2.txt", "r+"); if (fp == NULL) { printf("r+ : open error\n"); } return ; } void w_plus_access() { //测试存在的文件 FILE *fp = fopen("data.txt", "w+"); if (fp == NULL) { printf("w : failed to open file\n"); exit(1); } //向文件中输入内容,并且在输入前会清空文件中的内容 fprintf(fp, "w+ :hahahahah, world\n"); char s[100] = {0}; fscanf(fp, "%s", s); //对于这里为什么读取的内容为空,在后续代码会进行解释 printf("w+ : %s\n", s); fclose(fp); char file_name[100] = {0}; rand_file_name(file_name, 10); //当用w+打开不存在的文件时,会创建新的文件,并且文件名就是fopen的第一个参数 printf("w+ : open new name : %s\n", file_name); fp = fopen(file_name, "w+"); fclose(fp); return ; } int main() { srand(time(0)); r_access(); w_access(); r_plus_access(); w_plus_access(); return 0; }
对于这部分代码,从调用的函数顺序一个一个实现和测试,而不是把全部代码写上去在测试,并且最开始当前目录下是有一个data.txt文件的。我的data.txt文件最初内容是h