C语言基础之文件简单操作

文件操作

文件基本操作

读写方式作用
r:read ( fopen(“xx”,“r”))读的方式打开文件,如果不存在打开失败,fopen返回空
w:write ( fopen(“xx”,“w”))写的方式,如果文件不存在,创建文件,如果存在,清空原文件
a: append追加方式打开文件,如果原为有东西,在文件后面接着写,不存在就创建文件
r+可读可写,如果文件不存在打开失败
w+可读可写,如果文件不存在会自动创建一个文件
a+可读可写,如果文件不存在会打开失败,如果文件存在会在文件后面接着写(追加)
rb以二进制读打开文件
wb以二进制写打开文件
ab以二进制追加打开文件
rb+,wb+,ab+二进制的读写与追加

简单的文件操作

  • 用什么方式写的,就可以用什么方式读出来,和文件后缀没关系
#include <stdio.h>
int main() 
{
	FILE* fp = fopen("test.txt", "r");
    //FILE* fp = fopen("test.txt", "w");
    //FILE* fp = fopen("test.txt", "a");

	if(fp==NULL)
	{
		printf("文件打开失败!\n");
		return 0;
	}
	fclose(fp);    //不可以关闭一个空的文件
	return 0;
}

字符字符串的读写操作

字符读写操作

  • fgetc: 字符读

  • fputc: 字符写

  • 文件结束标记: EOF

  • 文件读写函数会完成文件指针移动

    #include <stdio.h>
    
    int main()
    {
    	FILE* fp = fopen("read.txt", "r");
    	int userKey = 0;
    	userKey = fgetc(fp);
    	//以字符的方式读文件
    	while (userKey != EOF) 
    	{
    		putchar(userKey);
    		userKey = fgetc(fp);
    	}
    	fclose(fp);
    	//以字符的方式写文件
    	FILE* write = fopen("read.txt", "a");
    	char str[] = { "你是一个好人!" };
    	int i = 0;
    	while (str[i] != '\0')
    	{
    		fputc(str[i], write);
    		i++;
    	}	
    	fclose(write);
    	return 0;
    }
    

字符串读写操作

  • fgets: 字符串读

  • fputs:字符串写

    #include <stdio.h>
    
    void deleteEnter(char* str) 
    {
    	int i = 0;
    	while (str[i] != '\0')
    		i++;
    	str[i - 1] = str[i];
    }
    
    int main()
    {
    	
    	//以字符串的方式操作
    	//fseek ftell
    	printf("\n");
    	char readStr[100][100];
    	FILE* readByStr = fopen("str.txt", "r");
    	//feof(readByStr); ||判断文件指针是否在文件末尾
    	int row = 0;
    	while (fgets(readStr[row], 100, readByStr))
    	{
    		//注意点:每一个读出来的字符串末尾是\n
    		/*
    			fgets(readStr[row], 100, readByStr)
    			if(feof(readByStr))
    			{
    				break;
    			}
    		*/
    		deleteEnter(readStr[row]); //自己手动去掉读出来的\n
    		puts(readStr[row]);
    		//printf("%s", readStr[row]);
    		row++;
    	}
    	fclose(readByStr);
    	FILE* writeByStr = fopen("str.txt", "a");
    	char strInput[] = "编程真有趣!!!";
    	fputs(strInput, writeByStr);
    	fclose(writeByStr);
    	return 0;
    }
    

格式化读写操作

  • 具有表格特性

  • fprint : 文件写操作

  • stdout : 标准输出

  • fscanf :文件读操作

    #include <stdio.h>
    /*
    	fprintf(FILE* fp,"格式控制字符",变量名表);
    	stdout:
    	fscanf(FILE* fp,"格式控制字符",变量名地址表);
    */
    struct MM 
    {
    	char name[20];
    	int age;
    	int num;
    };
    void writeInfoToFile(struct MM array[], int arrayNum, const char* fileName) 
    {
    	FILE* fp = fopen(fileName, "w");
    	for (int i = 0; i < arrayNum; i++) 
    	{
    		fprintf(fp, "%s\t%d\t%d\n", array[i].name, array[i].age, array[i].num);
    	}
    	fclose(fp);
    }
    void printMM(struct MM array[], int arrayNum) 
    {
    	printf("姓名\t年龄\t编号\n");
    	for (int i = 0; i < arrayNum; i++) 
    	{
    		printf("%s\t%d\t%d\n", array[i].name, array[i].age, array[i].num);
    	}
    }
    void readInfoFromFile(struct MM array[], const char* fileName) 
    {
    	FILE* fp = fopen(fileName, "r");
    	int i = 0;
    	while (fscanf(fp, "%s\t%d\t%d\n",array[i].name, &array[i].age, &array[i].num) != EOF) 
    	{
    		i++;
    	}
    	fclose(fp);
    }
    int main() 
    {
    	struct MM array[3] = { "baby",18,1001,
    	"anning",20,1003,
    	"susu",19,1005 };
    	writeInfoToFile(array, 3, "mm.txt");
    	struct MM readMM[3];
    	readInfoFromFile(readMM, "mm.txt");
    	printMM(readMM,3);
    	return 0;
    }
    

字节流形式读写操作——二进制读写操作

  • fread() :读

  • fwrite() :写

  • size_t fwrite(void const* _Buffer, size_t _ElementSize, size_t _ElementCount, FILE* _Stream);

    • const* _Buffer :要写的内容的首地址

    • size_t _ElementSize : 写多少

    • size_t _ElementCount :写几次

    • FILE* _Stream :写到那个文件中去

      #include <stdio.h>
      struct MM 
      {
      	char name[20];
      	int age;
      	int num;
      };
      int main() 
      {
      	struct MM array[3] = { "Moying",18,1001,
      	"Liqi",20,1003,
      	"Qiangsen",19,1005 };
      	FILE* fp = fopen("DK.txt", "wb");
      	//第一种写法:
      	//for (int i = 0; i < 3; i++) 
      	//{
      	//	fwrite(&array[i], sizeof(struct MM), 1, fp);
      	//}
      	fwrite(array, sizeof(struct MM), 3, fp);
      	fclose(fp);
      	FILE* read = fopen("mm.txt","rb");
      	struct MM readArray[3];
      	//for (int i = 0; i < 3; i++) 
      	//{
      	//	fread(readArray + i, sizeof(struct MM), 1, read);
      	//}
      	fread(readArray, sizeof(struct MM), 3, read);
      	for (int i = 0; i < 3; i++) 
      	{
      		printf("%s\t%d\t%d\n", readArray[i].name, 
      			readArray[i].age, readArray[i].num);
      	}
      	fclose(read);
      	char str[40000] = "";  //大文件的读写一定是动态内存申请
      	FILE* readPic = fopen("xx.jpg", "rb");
      	fread(str, 34937, 1, readPic);
      	fclose(readPic);
      	FILE* writePic = fopen("xxoo.jpg", "wb");
      	//for (int i = 1000; i < 2000; i++) 
      	//{
      	//	str[i] -= 1;
      	//}
      	fwrite(str, 34937, 1, writePic);
      	fclose(writePic);
      	return  0;
      }
      

文件指针操作

  • ftell :统计文件指针移动的字节数

  • fseek :定位文件指针

  • fseek(FILE* fp,long size,int mode)

    • *fp : 文件指针
    • long size :偏移的字节数
    • int mode :参照点(枚举类型)
      • SEEK_CUR 当前位置
      • SEEK_END 结束位置
      • SEEK_SET 开始位置
  • feof(FILE* fp) :判断文件指针是否在文件末尾

  • rewind(FILE* fp) :把文件指针恢复文件开始位置

    #include <stdio.h>
    int getFileSize(const char *fileName) 
    {
    	FILE* fp = fopen(fileName, "r");
    	//文件开始的位置
    	if (fp == NULL)
    		return 0;
    	fseek(fp, 0L, SEEK_END);  //移动到文件末尾
    	int size = ftell(fp);
    	fclose(fp);
    	return size;
    }
    int main() 
    {
    	printf("%d\n", getFileSize("xxoo.exe"));
    	int size = getFileSize("xxoo.exe");
    	char* pMemory = (char*)malloc(size + 1);
    	FILE* read = fopen("xxoo.exe", "r");
    	fread(pMemory, size, 1, read);
    	fclose(read);
    
    	//以字符的方式写文件
    	FILE* fp = fopen("read.txt", "w+");
    	char str[] = { "你是一个好人!" };
    	int i = 0;
    	while (str[i] != '\0')
    	{
    		fputc(str[i], fp);
    		i++;
    	}	
    	rewind(fp);
    	int userKey = 0;
    	userKey = fgetc(fp);
    	//以字符的方式读文件
    	while (userKey != EOF)
    	{
    		putchar(userKey);
    		userKey = fgetc(fp);
    	}
    	fclose(fp);
    	return 0;
    }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想写代码的懒大王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值