0
点赞
收藏
分享

微信扫一扫

【文本文件与二进制文件的区别;文件打开与关闭的方法、不同读写方式】(学习笔记19--文件上)

书写经典 2022-03-25 阅读 94
c语言

目录

文件的打开与关闭

文件的打开

FILE *fopen(const char *fname,const char *mode);
模式字符串功能
“r”以只读模式打开文本文件
“w”以只写模式打开文本文件
“a”以追加模式打开文本文件
“r+”以读写模式打开文本文件
“w+”以读写模式打开文本文件
“a+”以读写模式打开文本文件
“rb”以只读模式打开二进制文件
“wb”以只写模式打开二进制文件
“ab”以追加模式打开二进制文件
“rb+”以读写模式打开二进制文件
“wb+”以读写模式打开二进制文件
“ab+”以读写模式打开二进制文件
FILE *pfile = fopen("D:\\test.txt","r");
if(pfile != NULL)
	printf("File opened successfully.\n");
else
	printf("Failed to open file.\n");
File opened successfully.
Failed to open file.
FILE *pfile = fopen("D:\\test.txt","w");
FILE *pfile = fopen("D:\\test.txt","a");

文件的关闭

int fclose(FILE *stream);
FILE *pfile = fopen("D:\\test.txt","r");
if(pfile)
{
	printf("File opened successfully.\n");
	if(!fclose(pfile))
		printf("File closed successfully.\n");
	else
		printf("File closure failed successfully.\n");
}
else
	printf("Failed to open file.\n");
File opened successfully.
File closed successfully.
Failed to open file.

标准文件流

文件流的重定向

FILE *freopen(const char *fname,const char *mode,FILE *stream);
freopen("D:\\test.txt","w",stdout);
printf("Hello");
putchar(' ');
printf("World");
Hello World
char ch1,ch2;
char str[100];
freopen("D:\\test.txt","r",stdin);
scanf("%c",&ch1);
ch2 = getchar();
gets(str);
printf("ch1:%c\nch2:%c\nstr:%s\n",ch1,ch2,str);
ch1:H
ch2:e
str:llo World

文件的读写

以字符的方式读写文件

int fputc(int ch,FILE *stream);
#include <stdio.h>
int main()
{
	char str[] = "Red apple";
	FILE *pfile = fopen("D:\\test.txt","w");
	if(pfile)
	{
		char *ptmp = str;
		while(*ptmp)
		{
			fputc(*ptmp,pfile);
			++ptmp;
		}
		fclose(pfile);
		puts("Write to complete.");
	}
	else
		puts("File opening failed.");
	return 0;
}
int fflush(FILE *stream);
if(pfile)
	{
		char *ptmp = str;
		while(*ptmp)
		{
			fputc(*ptmp,pfile);
			++ptmp;
		}
		fflush(pfile);			//刷新输出缓冲区
		fclose(pfile);			//关闭文件
		puts("Write to complete.");
	}
int fgetc(FILE *stream);
#include <stdio.h>
int main()
{
	char buf[128];
	FILE *pfile = fopen("D:\\test.txt","r");
	if(pfile)
	{
		char *p = buf;
		while((*p = fgetc(pfile)) != EOF)
			++p;
		*p = '\0';
		fclose(pfile);
		printf("The read content is: %s\n",buf);
	}
	else
		puts("File opening failed.");
	return 0;
}
The read content is: Red apple

以行的方式读写文件

int fputs(const char *str,FILE *stream);
#include <stdio.h>
int main()
{
	FILE *pfile = fopen("D:\\test.txt","w");
	if(pfile)
	{
		if(fputs("Red appple",pfile) != EOF)
			printf("File written successful.\n");
		else
			printf(Failed to write file.\n);
		fclose(pfile);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
File written successful.
char *fgets(char *str,int num,FILE *stream);
#include <stdio.h>
int main()
{
	char buf[128];
	FILE *pfile = fopen("D:\\test.txt","r");
	if(pfile)
	{
		if(fgets(buf,128,pfile))
			printf("The read content is: %s\n",buf);
		else
			printf("Failed to read file.\n");
		fclose(pfile);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
The read content is: Red apple

以格式化的方式读写文件

int fprintf(FILE *stream,const char *format,...);
struct STU
{
	int num;
	char name[20];
	float score;
};
int main()
{
	struct STU stu = {100,"zhangsan",90.5};
	FILE *pfile = fopen("D:\\test.txt","w");
	if(pfile)
	{
		fprintf(pfile,"%d %s %.2f",stu.num,stu.name,stu.score);
		fclose(pfile);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
100 zhangsan 90.50
int sprintf(char *buffer,const char *format,...)
int main()
{
	struct STU stu = {100,"zhangsan",90.5};
	char buf[1024];
	sprintf(buf,"%d %s %.2f",stu.num,stu.name,stu.score);
	puts(buf);
	return 0;
}
100 zhangsan 90.50
int fscanf(FILE *stream,const char *format,...);
int main()
{
	struct STU stu;
	FILE *pfile = fopen("D:\\test.txt","r");
	if(pfile)
	{
		fscanf(pfile,"%d%s%f",&stu.num,stu.name,&stu.score);
		fclose(pfile);
		printf("num:%d\nname:%s\nscore:%.2f\n",stu.num,stu.name,stu.score);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
num:100
name:zhangsan
score:90.50

以块的方式读写文件

int fwrite(const void *buffer,size_t size,size_t count,FILE *stream);
#include <stdio.h>
struct STU
{
	int num;
	char name[20];
	float score;
};
int main()
{
	struct STU stu = {100,"zhangsan",90.5};
	FILE *pfile = fopen("D:\\test.dat","wb");
	if(pfile)
	{
		fwrite(&stu,sizeof(stu),1,pfile);
		fclose(pfile);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
int fread(void *buffer,size_t size,size_t num,FILE *stream);
#include <stdio.h>
struct STU
{
	int num;
	char name[20];
	float score;
};
int main()
{
	struct STU stu;
	FILE *pfile = fopen("D:\\test.dat","rb");
	if(pfile)
	{
		fread(&stu,sizeof(stu),1,pfile);
		fclose(pfile);
		printf("num:%d\nname:%s\nscore:%.2f\n",stu.num,stu.name,stu.score);
	}
	else
		printf("File opening failed.\n");
	return 0;
}
num:100
name:zhangsan
score:90.50

在这里插入图片描述

举报

相关推荐

文本文件读写

0 条评论