C语言文件

打开与关闭文件

  1. 定义一个指向文件型数据的指针变量
FILE *fp
  1. 打开文件的方式:
if((fp=fopen("file","r"))==NULL)
{
	printf("cannot open this file\n");
	exit(0);
}
文件使用方式含义如果指定文件不存在
r+为了读和写,打开一个文本文件(从文件开头开始写,覆盖原内容)出错
w+为了读和写,建立一个新的文本文件建立新文件
a+为了读和写,打开一个文本文件(追加写)出错
  1. 关闭数据文件
fclose(文件指针)

顺序读写数据文件

函数名调用形式功能返回值
fgetcfgetc(fp)从fp指向的文件读入一个字符读成功,带回所读的字符,失败则返回文件结束标志EOF
fputcfputc(ch, fp)把字符ch写到文件指针变量fp所指向的文件中输出成功,返回值就是输出的字符,输出失败,则返回EOF
格式化读写样例作用
fprintf (文件指针,格式字符串,输出列表)fprintf(fp,"%d,%6.2f",i,f)把int型的 i 和float型的 f 输出到 fp 指向的文件中
fscanf (文件指针,格式字符串,输入列表)fscanf(fp,"%d,%f",&i,&f)从 fp 指向的文件中读入ASCII字符

例子
  1. 现有一文件“student.dat”,该文件存放学生的数据包括:学号,姓名,性别,年龄,地址。编写程序实现将学号、姓名、住址单独提取出来(其中不包含学号“2001”的学生)另建一个文件。
#include<stdio.h>
#include<cstring>
#include<cstdlib>
struct Student
{
    int num;
    char name[20];
    char sex[20];
    int age;
    char addr[20];
}stu[50];

int main()
{
    int i=0;
    int j;
    FILE *fp,*fq;
    if((fp=fopen("student.txt","r+"))==NULL)
    {
        printf("cannot open this file");
        exit(0);
    }
    while((fscanf(fp,"%d%s%s%d%s",&stu[i].num,stu[i].name,stu[i].sex,&stu[i].age,stu[i].addr))!=EOF)
    {
        if(stu[i].num!=2001)
            i++;
    }
    if((fq=fopen("sturesult.txt","w+"))==NULL)
    {
        printf("wrong");
        exit(0);
    }
    for(j=0;j<i;j++)
        fprintf(fq,"%d %s %s\n",stu[j].num,stu[j].name,stu[j].addr);
    return 0;
}


  1. 实现从磁盘文件“d:\stu.dat”中读出全部学生信息(学号、姓名、性别、成绩),分别统计高于平均成绩和低于平均成绩的人数,并将结果写入到“d:\Result.dat”文件中。
#include<stdio.h>
#include<cstdlib> //exit(0)需要
#include<cstring>
#define SIZE 10
struct Student
{
	int num;
	char name[20];
	char sex[20];
	int score;
}stu[20];

int main()
{
	int i=0;
	int j;
	int sum=0;
	float average;
	int high=0;
	int low=0;
	int n=0;
	FILE *fp;
	if((fp=fopen("stu.txt","r+"))==NULL)
	{
		printf("cannot open the file");
		exit(0);
	}
	while(fscanf(fp,"%d%s%s%d",&stu[i].num,stu[i].name,stu[i].sex,&stu[i].score)!=EOF)
	{
		n++;
		i++; 
	} 
	for(i=0;i<n;i++)
		sum+=stu[i].score;
	average=sum/n;
	for(i=0;i<n;i++)
	{
		if(stu[i].score>average)
			high++;
		else 
			low++;
	}
	
	if((fp=fopen("result.txt","w+"))==NULL)
	{
		printf("cannot open");
		exit(0);
	}
	fprintf(fp,"%d  %d",high,low);
	printf("%d %d",high,low);
	return 0;
}


  1. 现有两个学生信息文件“studentA.dat”和“studentB.dat”,每个文件中各有10名学生信息(学号,姓名,性别,年龄,住址)将两个文件中的学生信息按照学号从小到大顺序合并,合并结果写入到“studentC.dat”中。
#include<stdio.h>
#include<cstdlib>
#include<cstring>
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	char address[20];
}stu[20];
int main()
{
	FILE *fp,*fq; 
	struct Student temp;
	int i=0;//数据数量
	int j=0,k=0;
	//打开文件 
	if((fp=fopen("studentA.dat","r+"))==NULL)
	{
		printf("cannot open the file");
		exit(0);
	}
	if((fq=fopen("studentB.dat","r+"))==NULL)
	{
		printf("cannot open the file");
		exit(0);
	}
	//读文件 
	while(fscanf(fp,"%d%s%c%d%s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].address)!=EOF)
	{
		i++; 
	}
	while(fscanf(fq,"%d%s%c%d%s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].address)!=EOF)
	{
		i++; 
	}
	//按学号排序
	for(j=0;j<i-1;j++) //排序 
	{
		for(k=i-1;k>j;k--)
		{
			if(stu[k].num>stu[k-1].num)
			{
				temp=stu[k];
				stu[k]=stu[k-1];
				stu[k-1]=temp;
			}
		}
	}
	fclose(fp);
	fclose(fq);
	//打开文件 
	if((fp=fopen("studentC.dat","w+"))==NULL)
	{
		printf("cannot open");
		exit(0);
	}
	//写文件 
	for(j=0;j<i;j++)
		fprintf(fp,"%d%s%c%d%s",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].address);
	fclose(fp);
	return 0;
}

  1. 将已知文件中的内容转换成字符串,并过滤掉大写字母和数字,完成后写回。
#include<stdio.h>
#include<cstring>
#include<cstdlib>
int main()
{
    FILE *fp;
    char a[500];
    int i=0;
    int j;
    char ch;
    if((fp=fopen("c.txt","r+"))==NULL) //打开文件
    {
        printf("cannot open the file");
        exit(0);
    }
    while((ch=fgetc(fp))!=EOF) //提取文件中所有符合要求的字符
    {
        if(((ch>='A')&&(ch<='Z'))||((ch>='0')&&(ch<='9')))
            continue;
        else if((ch==' ')||(ch=='\n'))
            continue;
        else
        {
            a[i]=ch;
            i++;
        }
    }
    fclose(fp);
    if((fp=fopen("c.txt","w+"))==NULL)
    {
        printf("cannot open the file");
        exit(0);
    }
    fprintf(fp,"%s",a); //输出字符串到文件
    return 0;
}
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值