c语言文件操作

本文详细解析了C语言中文件的分类(程序文件与数据文件)、文件名的构成、文件指针的作用,以及各种文件操作函数如fopen、fclose、fread、fwrite、fseek和ftell的用法。特别关注了文件的打开模式、随机读写和结束标志的判断。
摘要由CSDN通过智能技术生成

什么是文件

我们一般谈的文件有两种:程序文件、数据文件
程序文件:
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。
数据文件:
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。

文件名

一个文件要有一个唯一的文件标识,以便用户识别和引用。
文件名包含3部分:文件路径+文件名主干+文件后缀
例如: c:\code\test.txt
为了方便起见,文件标识常被称为文件名。

文件指针

缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。
每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE.

文件的打开和关闭

fopen的函数原型

FILE *fopen( const char *filename, const char *mode );

使用的方式 含义 如果指定文件不存在
“r”(只读) 为了输入数据,打开一个已经存在的文本文件 出错
“w”(只写) 为了输出数据,打开一个文本文件 建立一个新的文件
“a”(追加) 向文本文件尾添加数据 出错
“rb”(只读) 为了输入数据,打开一个二进制文件 出错
“wb”(只写) 为了输出数据,打开一个二进制文件 建立一个新的文件
“ab”(追加) 向一个二进制文件尾添加数据 出错
“r+”(读写) 为了读和写,打开一个文本文件 出错
“w+”(读写) 为了读和写,建议一个新的文件 建立一个新的文件
“a+”(读写) 打开一个文件,在文件尾进行读写 建立一个新的文件
“rb+”(读写) 为了读和写打开一个二进制文件 出错
“wb+”(读写) 为了读和写,新建一个新的二进制文件 建立一个新的文件
“ab+”(读写) 打开一个二进制文件,在文件尾进行读和写 建立一个新的文件。
eg:

int main()
{
	FILE* pf=fopen("TestDemo2.java", "r");
	if (pf == NULL)
	{
		printf("打开文件失败\n");
		printf("%s\n", strerror(errno));//提示错误内容
		return 1;
	}
	//打开文件成功
	printf("打开文件成功");
	//读写文件
	fclose(pf);
	pf = NULL;
	return 0;
}
fopen函数如果以写的形式打卡
如果文件不存在,会创建这个文件
如果文件存在,会清空文件的内容
fopen函数如果是以读的方式打开
文件不存在,打开失败

fclose的函数原型:

int fclose(FILE *stream);

文件的顺序读写

c语言的程序,运行起来的时候,会默认打开3个流:
stdin-标准输入流(键盘)
stdout-标准输出流(屏幕)
stderr-标准错误流(屏幕)
功能 函数名 适用于
字符输入函数 fgetc 所有输入流
字符输出函数 fputc 所有输出流
文本行输入函数 fgets 所有输入流
文本行输出函数 fputs 所有输出流
格式化输入函数 fscanf 所有输入流
格式化输出函数 fprint 所有输出流
二进制输入 fread 文件
二进制输出 fwrite 文件

int main()
{
	//fopen函数如果以写的形式打卡
	//如果文件不存在,会创建这个文件
	//如果文件存在,会清空文件的内容
	//fopen函数如果是以读的方式打开
	//文件不存在,打开失败
	FILE* pf=fopen("data.txt", "w");
	if (pf == NULL)
	{
		printf("%s\n", strerrno(errno));
		return 1;
	}
	//写文件
	int i = 0;
	for (i = 'a'; i <= 'z'; i++)
	{
		fputc(i, pf);
	}
	int ch = 0;
	while ((ch = fgetc(pf) != EOF))
	{
		printf("%c ", ch);
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

1.fputc的使用
函数原型

int fputc( int c, FILE *stream );

int main()
{
	FILE * pf = fopen("data.txt", "w");
	if (pf == NULL)
	{
		printf("%s\n", strerrno(errno));
		return 1;
	}
	//写一行数据
	fputs("hello bit\n", pf);
	return 0; 
}

2.fgetc的使用
函数原型

int fgetc( FILE *stream );

该函数返回读取的字符的ascii码值, 读取错误返回 EOF

int fgetc( FILE *stream );

#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return -1;
	}
	// 打开文件成功,读文件
	int ch = 0;
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c\n", ch);
	}
	// 关闭文件
	fclose(pf);
	pf = NULL;
}

3.fgets
函数原型:

char *fgets( char *string, int n, FILE *stream );

函数功能:
Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.
fgets函数从输入流参数中读取字符串并将其存储在字符串中。

int main()
{
	char arr[1024] = { 0 };
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno);
		return 1;
	}
	fgets(arr, 100, pf);
	printf("%s\n", arr);
	fclost(pf);
	pf = NULL;
	return 0;  
}

4.fputs
函数原型:

int fputs( const char *string, FILE *stream );

功能:如果成功,函数返回一个非负值。出错时,fputs返回EOF。

#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
	FILE* pf = fopen("data.txt", "w");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return -1;
	}
	fputs("hello\n", pf); // pf也可改成 stdout
	fputs("hello world\n", pf); // pf 也可改成 stdout
	// 关闭文件
	fclose(pf);
	pf = NULL;
}

5.fprintf
函数原型;

int fprintf( FILE *stream, const char *format [, argument ]...);

功能:fprintf和printf不同的是前面加了文件名。

#include<stdio.h>
#include<errno.h>
#include<string.h>
struct S
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct S s = { "zhangsan",18,90.45f};
	FILE* pf = fopen("data.txt", "w");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return -1;
	}
	// 格式化写入
	fprintf(pf, "%s %d %f", s.name, s.age, s.score);
	// 关闭文件
	fclose(pf);
	pf = NULL;
}

6.fscanf
函数原型:

int fprintf( FILE *stream, const char *format [, argument ]...);

函数功能:
fscanf适用于所有流,与scanf相比多了输入流参数。

#include<stdio.h>
#include<errno.h>
#include<string.h>
struct S
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct S s = { 0 };
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return -1;
	}
	// 格式化读取
	fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.score));
	printf("%s %d %f", s.name, s.age, s.score);
	// 关闭文件
	fclose(pf);
	pf = NULL;
}

7.sscanf
函数原型:

int sscanf( const char *buffer, const char *format [, argument ] ... );

功能:
Read formatted data from a string.

struct Stu
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct Stu s = { "zhangsan", 20, 66.5f };
	char buf[200] = { 0 };
	sprintf(buf, "%s %d %f", s.name, s.age, s.score);
	printf("%s\n", buf);
}//按照字符串的形式打印

8.sprintf
函数原型:

int sprintf( char *buffer, const char *format [, argument] ... );

功能:
Write formatted data to a string.

struct Stu
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct Stu tmp = { 0 };
	sscanf(buf, "%s %d %f", tmp.name, &(tmp.age), &tmp.score);
	printf("%s %d %f\n", tmp.name, tmp.age, tmp.score);
	return 0;
}

9.fwrite
函数原型

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

功能:
Writes data to a stream.
buffer : 指向要写入的数据的指针
size : 数据大小(字节)
count : 要写入的最大数据数
stream : 指向文件结构的指针

int main()
{
	struct Stu s = { "zhangsan", 20, 99.5 };
	FILE* pf = fopen("data.txt", "wb");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno);
		return 1;
	}
	//写文件
	fwrite(&s, sizeof(s), 1, pf);
	fclose(pf);
	pf = NULL;
	return 0;
	return 0;
}

10.fread
函数原型:

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

功能:Reads data from a stream.

struct Stu
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct Stu s = { "zhangsan", 20, 99.5 };
	FILE* pf = fopen("data.txt", "wb");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno);
		return 1;
	}
	//写文件
	//fwrite(&s, sizeof(s), 1, pf);
	//读文件
	fread(&s, sizeof(struct Stu), 1, pf);
	fclose(pf);
	pf = NULL;
	return 0;
	return 0;
}

文件的随机读写

fseek
函数原型:

int fseek( FILE *stream, long offset, int origin );

根据文件指针的位置和偏移量来定义文件位置
函数功能:
Moves the file pointer to a specified location.

	int main()
	{
		FILE* pf = fopen("data.txt", "r");
		if (pf == NULL)
		{
			printf("%s", strerror(errno));
			return 1;
		}
		//读取
		int ch = fgetc(pf);
		printf("%c\n", ch);
		ch = fgetc(pf);
		printf("%c\n", ch);
		//定位文件指针到文件的起始位置
		fseek(pf, -2, SEEK_CUR);
		fclose(pf);
		pf = NULL;
		return 0;
	}

ftell
函数原型:

int fseek( FILE *stream, long offset, int origin );

功能:
返回指针相对于起始位置的偏移量

	int main()
	{
		FILE* pf = fopen("data.txt", "r");
		if (pf == NULL)
		{
			printf("%s", strerror(errno));
			return 1;
		}
		//读取
		int ch = fgetc(pf);
		printf("%c\n", ch);
		ch = fgetc(pf);
		printf("%c\n", ch);
		printf("%c\n",ftell(pf));
		//定位文件指针到文件的起始位置
		fseek(pf, -2, SEEK_CUR);
		fclose(pf);
		pf = NULL;
		return 0;
	}

文件读取结束的标志

牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。
ferror
函数原型:

int ferror( FILE *stream );

函数功能:
Tests for an error on a stream.

  1. 文本文件读取是否结束,判断返回值是否为EOF (fgetc),或者NULL(fgets)
    例如:
    fgetc判断是否为EOF.
    fgets判断返回值是否为NULL.
  2. 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。
    例如:
    fread判断返回值是否小于实际要读的个数。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int c; // 注意:int,非char,要求处理EOF
    FILE* fp = fopen("test.txt", "r");
    if(!fp) {
        perror("File opening failed");
        return EXIT_FAILURE;
   }
 //fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
    while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
   { 
       putchar(c);
   }
 //判断是什么原因结束的
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");
    fclose(fp);
}
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值