文件的学习

文件:

  1. 源程序文件:储存在磁盘的程序代码的集合
  2. 目标文件:源程序文件编译后生成的文件
  3. 可执行文件:目标文件经过连接而生成的文件
  4. 数据文件:保存着待处理或已经处理的文件

按数据的储存格式分:

1. 文本文件:ASCII文件

每个字节存放相应字符的ASCII代码值,代表一个字符

10000
分别存放‘1’ ‘0’ ‘0’ ‘0’ ‘0’ ‘0’的代码值(0->48)
00110001 00110000 00110000 00110000 00110000
2.二进制文件:

把数据按在内存中的储存形式(机内码)原样输出到磁盘上存放

10000
int类型在内存中占4字节
00000000 00000000 00100111 00010000

1.文件类型指针:

FILE*fp

2.打开文件:

FILE*fopen(char* file_name,char*mode)
1. r:只能读数据,且文件必须存在
2. w:只能写数据

文件不存在,会创建
文件存在,先删掉再写

3. a:追加数据,但不能读取数据
4.r+ w+ a+以先后顺序打开文件,1.2.3规则同样适用
5.rb(+) wb(+) ab(+)表示打开的是二进制文件

打开文件错误,建议按以下方法写代码:

if((fp=fopen("d:\\data\\sushu.txt""w+"))==NULL
{
	printf(cannot open file d:\\data\\sushu,txt!\n";
	exit(0);
}

3.关闭文件:

int fclose (FILE*fp)

返回0文件正常关闭
返回非0表示出错

4.文本文件读写:

1. 字符输入函数 fgetc

int fgetc (FILE*fp)

读入当前文件指针所指的字符(文件必须以读或读写打开),返回改字符的ASCII值,读到文件结束标志,返回EOF(-1)

一般用法:

char c;
c=fgetc(fp);

2. 字符输出函数 fputc

int fputc (char ch, FILE*fp)

在当前文件指针所指的位置写入一个字符,写入成功返回改字符的ASCII值,不成功返回EOF

一般用法:

fputc (ch,fp)

3.feof

int feof (FILE *fp)

读到文件末尾返回非零,否则返回0
一般用法:

while(!feof(fp)              //执行直到文件结束
{
	...
}

文本文件: 可用EOF或feof函数判断文件是否结束
二进制文件: 只能用feof函数(每个字节中的二进制可能就是-1)

创建一个text.txt文件:

#include<stdio.h>
#include<stdlib.h>

int main()
{
	FILE* fp;
	int ch;
	if (!(fp = fopen("C:\\Users\\20954\\Desktop\\VS\\text.txt", "w")))
	{
		printf("error");
		exit(1);
	}
	ch = getchar();
	while(ch != EOF)
	{
		fputc(ch, fp);
		ch = getchar();
	}
	fclose(fp);
	return 0;
}

将上述文件读入并打印出来:

#include<stdio.h>
#include<stdlib.h>

int main()
{
	FILE* fp;
	int ch;
	if (!(fp = fopen("C:\\Users\\20954\\Desktop\\VS\\text.txt", "r")))
	{
		printf("error");
		exit(1);
	}
	ch = fgetc(fp);
	while(ch != EOF)
	{
		printf("%c",ch);
		ch = fgetc(fp);
	}
	fclose(fp);
	return 0;
}

文件拷贝:

#include<stdio.h>
#include<stdlib.h>

static void copyFile(FILE* source, FILE* intention)//见下文fgets
{
	int ch;
	while ((ch = getc(source)) != EOF) putc(ch, intention);
}
/*
#define maxLine 100
static void copyFile(FILE *source, FILE *intention)
{
    char buffer[maxLine];
    while((fgets(buffer,maxLine,source))!=NULL)
        fputs(buffer,intention);
}
*/
static FILE* openFile(char* prompt, char* mode)
{
	char tmpFile[20];
	FILE* reval = NULL;
	while (1)
	{
		printf("%s", prompt);
		gets(tmpFile);
		reval = fopen(tmpFile, mode);
		if (reval != NULL) break;
		printf("Can't open the file\"%s\"\n", tmpFile);
		exit(1);
	}
	return reval;
}

int main()
{
	FILE* source, * intention;
	source = openFile("source:", "r");
	intention = openFile("intention:", "w");
	copyFile(source, intention);
	fclose(source);
	fclose(intention);
	return 0;
}

4.字符串输入函数fgets

char*fgets(char*str,int n,FILE*fp)

从fp所指向的文件的当前读写位置起,最多读n-1(包括换行符和文件结束标志,读到它们时终止)个字符,添加后缀 ’\n’ 后复制到字符数组str中
成功返回str首地址
失败返回NULL

移动到该字符串的下一个字符串前

5.字符串输出函数fputs

int fputs (char*str,FILE*fp)

向文件内写str (不包括 \n)
成功返回最后一个字符的ASCII值
失败返回0

移动到写入的字符串之后

fputs(str,fp);

不会自动再输出字符串到文件时添加 \n
需要时可以加一条

fputc('\n',fp);

前文copy函数可改为

#define maxLine 100
static void copyFile(FILE *source, FILE *intention)
{
    char buffer[maxLine];
    while((fgets(buffer,maxLine,source))!=NULL)
        fputs(buffer,intention);
}

6.格式化输入函数 fscanf

int fscanf(FILE*fp, char*format, 地址)

7.格式化输出函数 fprintf

int fprintf(FILE*fp, char*format, 地址)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值