C语言:文件指针

本文详细探讨了C语言中的文件指针概念,包括其在文件操作中的作用,如何打开、关闭文件,以及如何使用fread()和fwrite()等函数通过文件指针进行数据读写。通过对文件指针的深入理解,读者将能更好地掌握C语言的文件处理能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<windows.h>

//标准输入和标准输出
int main()
{
	char buffer[10];
	int n = fread(buffer,sizeof(char),10,stdin);//stdin相当于键盘
	fwrite(buffer,sizeof(char),n,stdout);//stdout相当于屏幕
	//加头文件<windows.h>
	system("pause");//getchar()会在缓冲区读入\n,system()直接停止程序,不会读入\n
	return 0;
}


//写视频
//void CopyMp4(const char *dest,const char *src)
//{
//	assert(dest != NULL && src != NULL);
//	FILE *fr = fopen(src,"rb");
//	FILE *fw = fopen(dest,"wb");
//	assert(fr != NULL && fw != NULL);
//	char ch;
//	int len = 0;
//	while( (len = fread(&ch,sizeof(char),1,fr)) > 0 )
//	{
//		fwrite(&ch,sizeof(char),len,fw);
//	}
//	fclose(fr);//关闭读文件流
//	fclose(fw);//关闭写文件流
//}
//int main()
//{
//	char *srcpath = "C:\\Users\\lenovo\\Desktop\\11.mp4";
//	char *destpath = "D:\\22.mp4";
//	CopyMp4(destpath,srcpath);
//	getchar();
//	return 0;
//}


//指针在文件中移动,fseek指针位置
//int main()
//{
//	const char *path = "C:\\Users\\lenovo\\Desktop\\lz.txt";
//	FILE *fr = fopen(path,"r");
//	assert(fr != NULL);
//	char ch;
//	while(fread(&ch,sizeof(char),1,fr)>0)
//	{
//		putchar(ch);//打印字符,不会换行
//	}
//	printf("\n");
//	//fseek(),移动的指针,指针偏移量,偏移起始位置
//	//SEEK_CUR 文件指针的当前位置。
//	//SEEK_END 文件结尾。
//	//SEEK_SET 文件开头
//	fseek(fr,3,SEEK_SET);//负号向前移动,正的向后移动
//	while(fread(&ch,sizeof(char),1,fr)>0)
//	{
//		putchar(ch);//打印字符,不会换行
//	}
//	getchar();
//	return 0;
//}

//读文件
//size_t fread( 
//   void *buffer,//读到的数据,放到这里
//   size_t size,//一次读几个字节
//   size_t count,//总共读几次
//   FILE *stream //读哪个文件的);
//int main()
//{
//	const char *path = "C:\\Users\\lenovo\\Desktop\\lz.txt";//选择途径
//	FILE *fr = fopen(path,"r");//打开文件,指针遍历
//	assert(fr != NULL);//文件不能为空
//	char buffer[100] = {};
//	fread(buffer,sizeof(char),100,fr);
//	printf("%s\n",buffer);
//	
//	getchar();
//	return 0;
//}

//写文件
//size_t fwrite(
//   const void *buffer,//往文件内写的数据
//   size_t size,//每次写多少字节
//   size_t count,//总共写多少次
//   FILE *stream //写到哪个文件里面);
//int main()
//{
//	const char *path = "D:\\b.txt";
//	FILE  *fw = fopen(path,"w");//以二进制的形式写,如果有文件,
//	//将原来文件的内容清除掉,如果没有,自己创建文件
//	assert(fw != NULL);
//	char *str = "here is tulun";
//	fwrite(str,sizeof(char),strlen(str),fw);
//	printf("%s\n",str);
//
//	getchar();
//	return 0;
//}

//读写文件
//int main()
//{
//	const char *path = "D:\\b.txt";
//	FILE  *fw = fopen(path,"wb");//以二进制的形式写,写b
//	assert(fw != NULL);
//	const char *path2 = "D:\\b.txt";
//	FILE  *fr = fopen(path2,"rb");//以二进制的形式写,读b
//	int a = 10;
//	fwrite(&a,sizeof(int),1,fw);//把a写进去
//	fclose(fw);//关闭文件
//	int b = 0;
//	fread(&b,sizeof(int),1,fr);//把读到的10给b
//	printf("%d\n",b);
//	fclose(fr);//关闭文件
//
//	getchar();
//	return 0;
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值