C语言文件基础知识总结+几个例题

一.文件中的几个基本函数:

1.fscanf:

int fscanf ( FILE * stream, const char * format, ... );

2.fprintf:

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

这两个函数与格式化输入中的scanf与printf的区别就在于最前面加了一个文件指针,具体用法其实很类似。

3.fgetc:

int fgetc ( FILE * stream );

这与格式化输入中的getchar一个意思。 

4.fputc:

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

这与格式化输出中的putchar一个意思。 

 5.fgets:

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

字符串输入,与gets用法基本一致。 

6.fputs:

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

字符串输出,与puts用法基本一致

7.fread:

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

参数ptr 指向目标地址;

size 为一个元素的字节大小;

count 为读取的元素个数;

stream 为已打开的文件指针;

一般用于二进制的读入;

8.fwrite:

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

参数表示与fread一致。

也一般用于二进制的输出。

 

二.文件使用参数表

 

1.//"r"(只读):只能从文件中读数据,该文件必须先存在,否则打开失败。
2.//"w"(只写):只能向文件写数据,若指定的文件不存在则创建它,如果存在则先删除它再重建一个新文件。
3.//"a"(追加):向文件增加新数据(不删除原有数据),若文件不存在则打开失败,打开时位置指针移到文件末尾。
4.//"rb"(只读):只能从文件中读数据,该文件二进制文件必须先存在,否则打开失败。
5.//"wb"(只写):向文件写数据,若指定的二进制文件不存在则创建它,如果存在则先删除它再重建一个新文件。
6.//"ab"(追加):向二进制文件尾部添加数据,若文件不存在则打开失败,打开时位置指针移到文件末尾。
7.//"r+":可读/写数据,该文件必须先存在,否则打开失败。
8.//"w+":可读/写数据,用该模式打开新建一个文件,先向该文件写数据,然后可读取该文件中的数据。
9.//"a+":可读/写数据,原来的文件不被删去,位置指针移到文件末尾。

三.程序操作文件步骤 

 

 

 

 四.具体例题运用

 1.从键盘输入一个字符串,将其中小写字母全部换成大写字母,然后输入到一个磁盘文件test中保存。输入字符串以“!”结束。
 

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

int main(void)
{
	int k = 0;
	FILE* fp = fopen("test.txt","w");
	if(fp == NULL)
	{
		printf("can't open file!\n");
		return 0;
	}
	char str[1001];
	char ch;
	ch = getchar();
	while(ch != '!')
	{
		if(ch >= 'a' && ch <= 'z')
		{
			ch -= 32;
		}
		fputc(ch,fp);
		str[k++] = ch;
		ch = getchar();
	}
	fclose(fp);
	fp = fopen("test.txt","r");
	fgets(str,strlen(str)+1,fp);
	printf("%s",str);
	return 0;
}

2.

题目描述

某班开设有英语、数学、程序设计三门课程,编写程序读入该班同学信息,排序后按总成绩保存在文本文件中result.txt。该班同学不多于50人。

输入描述

每行数据描述一个同学信息,包括名字、英语、数学、程序设计成绩

输出描述

该班同学总成绩排序,成绩相同时按输入次序。 每项占12位。

样例输入

张三    80 90 88 
李四    66 80 90
王五    50 78 87
Jack     68 86 70
Tom     60 88 96

样例输出

文本文件result.txt,内容格式如下
        Name    English     Math       C        Total
        张三          80          90          88         258
         Tom          60          88          96         244
        李四          66          80          90         236
        Jack          68          86          70         224
        王五          50          78          87         215
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct students
{
	char name[20];
	int eng;
	int math;
	int c;
	int total;
}stu[51];

int main(void)
{
	int i = 1;
	while (scanf("%s%d%d%d",stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].c) != EOF)
	{
		stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
		i++;
	}
	stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
	for (int j = 1; j < i; j++)
	{
		for (int k = j + 1; k <= i; k++)
		{
			if (stu[j].total < stu[k].total)
			{
				struct students tem;
				tem = stu[j];
				stu[j] = stu[k];
				stu[k] = tem;
			}
		}
	}
	FILE* fp = fopen("result.txt","wt");
	fprintf(fp, "         Name    English     Math       C        Total\n");
	for (int j = 1; j < i; j++)
	{
		fprintf(fp,"%12s%12d%12d%12d%12d\n", stu[j].name, stu[j].eng, stu[j].math, stu[j].c, stu[j].total);
	}
	fclose(fp);
	return 0;
}

 3.

题目描述

某班开设有英语、数学、程序设计三门课程,该班同学信息保存在文本文件student.txt中,编写程序从文件读入该班同学信息,再分别按总成绩和英语成绩排序。该班同学不多于50人。

输入描述

文本文件student.txt,每行数据描述一个同学信息,包括名字、英语、数学、程序设计成绩

输出描述

该班同学总成绩排序和英语成绩排序,题目保证成绩不相同。 每项占12位。

样例输入

文本文件student.txt,内容格式如下
张三    80 90 88 
李四    66 80 90
王五    50 78 87
Jack     68 86 70
Tom     60 88 96

样例输出

        Name    English     Math       C        Total
        张三          80          90          88         258
         Tom          60          88          96         244
        李四          66          80          90         236
        Jack          68          86          70         224
        王五          50          78          87         215


        Name    English     Math       C        Total
        张三          80          90          88         258
        Jack          68          86          70         224
        李四          66          80          90         236
         Tom          60          88          96         244
        王五          50          78           87         215
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct students
{
	char name[20];
	int eng;
	int math;
	int c;
	int total;
}stu[51];

int main(void)
{
	int i = 1;
	FILE* fp = fopen("student.txt","rt");
	while (fscanf(fp,"%s%d%d%d",stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].c) != EOF)
	{
		stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
		i++;
	}
	stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
	for (int j = 1; j < i; j++)
	{
		for (int k = j + 1; k <= i; k++)
		{
			if (stu[j].total < stu[k].total)
			{
				struct students tem;
				tem = stu[j];
				stu[j] = stu[k];
				stu[k] = tem;
			}
		}
	}
	printf("         Name    English     Math       C        Total\n");
	for (int j = 1; j < i; j++)
	{
		printf("%12s%12d%12d%12d%12d\n", stu[j].name, stu[j].eng, stu[j].math, stu[j].c, stu[j].total);
	}
    for (int j = 1; j < i; j++)
	{
		for (int k = j + 1; k <= i; k++)
		{
			if (stu[j].eng < stu[k].eng)
			{
				struct students tem;
				tem = stu[j];
				stu[j] = stu[k];
				stu[k] = tem;
			}
		}
	}
	printf("         Name    English     Math       C        Total\n");
	for (int j = 1; j < i; j++)
	{
		printf("%12s%12d%12d%12d%12d\n", stu[j].name, stu[j].eng, stu[j].math, stu[j].c, stu[j].total);
	}
	return 0;
}

4. 

题目描述

某班开设有英语、数学、程序设计三门课程,编写程序读入该班同学信息,按总成绩非递增排序后保存在二进制文件中result.dat中。该班同学不多于50人。

输入描述

每行数据描述一个同学信息,包括名字、英语、数学、程序设计成绩

输出描述

二进制文件result.dat,总成绩非递增排序后该班同学信息,总成绩相同时按输入次序排列,每位同学信息包含:名字(20字节字符数组),后4项,每项4字节整形,分别表示英语、数学、程序设计三门课程和总成绩。

样例输入

张三    80 90 88 
李四    66 80 90
王五    50 78 87
Jack     68 86 70
Tom     60 88 96

样例输出

二进制文件result.dat,由若干同学信息组成,每位同学信息包含:名字(20字节字符数组),后4项,每项4字节整形,分别表示英语、数学、程序设计三门课程和总成绩。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct students
{
	char name[20];
	int eng;
	int math;
	int c;
	int total;
}stu[51];

int main(void)
{
	int i = 1;
	while (scanf("%s%d%d%d",stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].c) != EOF)
	{
		stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
		i++;
	}
	stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
	for (int j = 1; j < i; j++)
	{
		for (int k = j + 1; k <= i; k++)
		{
			if (stu[j].total < stu[k].total)
			{
				struct students tem;
				tem = stu[j];
				stu[j] = stu[k];
				stu[k] = tem;
			}
		}
	}
	FILE* fp = fopen("result.dat","wb");
    for (int j = 1; j <= i; j++)
	{
		fwrite(stu[j].name, sizeof(char), 20, fp);
		fwrite(&stu[j].eng, sizeof(int), 1, fp);
		fwrite(&stu[j].math, sizeof(int), 1, fp);
		fwrite(&stu[j].c, sizeof(int), 1, fp);
		fwrite(&stu[j].total, sizeof(int),1, fp);
	}
	fclose(fp);
	return 0;
}

5.

题目描述

某班开设有英语、数学、程序设计三门课程,编写程序,从学生信息文件student.dat中读入该班同学信息,按英语成绩非递增排序后输出。该班同学不多于50人。

输入描述

二进制文件student.dat,包含该班同学信息,每位同学信息包含:名字(20字节字符数组),后4项,每项4字节整形,分别表示英语、数学、程序设计三门课程和总成绩。

输出描述

按英语成绩非递增排序后该班同学信息,英语成绩相同时按输入次序排列。

样例输入

二进制文件student.dat,由若干同学信息组成,每位同学信息包含:名字(20字节字符数组),后4项,每项4字节整形,分别表示英语、数学、程序设计三门课程和总成绩。

样例输出

        Name    English     Math       C        Total
        张三          80          90          88         258
        Jack          68          86          70         224
        李四          66          80          90         236
         Tom          60          88          96         244
        王五          50          78          87         215
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct students
{
	char name[20];
	int eng;
	int math;
	int c;
	int total;
}stu[51];

int main(void)
{
	int i = 1;
	FILE* fp = fopen("student.dat","rb");
	while(feof(fp) != -1)
    {
        if(fread(stu[i].name,sizeof(char),20,fp) != 20)
        {
            break;
        }
        fread(&stu[i].eng,sizeof(int),1,fp);
        fread(&stu[i].math,sizeof(int),1,fp);
        fread(&stu[i].c,sizeof(int),1,fp);
        fread(&stu[i].total,sizeof(int),1,fp);
        i++;
    }
    stu[i].total = stu[i].math + stu[i].eng + stu[i].c;
    for (int j = 1; j < i; j++)
	{
		for (int k = j + 1; k <= i; k++)
		{
			if (stu[j].eng < stu[k].eng)
			{
				struct students tem;
				tem = stu[j];
				stu[j] = stu[k];
				stu[k] = tem;
			}
		}
	}
	printf("         Name    English     Math       C        Total\n");
	for (int j = 1; j < i; j++)
	{
		printf("%12s%12d%12d%12d%12d\n", stu[j].name, stu[j].eng, stu[j].math, stu[j].c, stu[j].total);
	}
	return 0;
}

 分享就到这里吧,希望能给大家带来帮助,如果感觉有用的话可以给点个赞哦!

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值