文件的打开(fopen函数)
FILE *fp;
fp=fopen(文件名,使用文件的方式)
文件使用方式 含义
"r"(只读) 为输入打开一个文本文件
"w"(只写) 为输出打开一个文本文件
"a"(追加) 向文本文件尾增加数据
"rb" 为输入打开一个二进制文件
"wb" 为输出打开一个二进制文件
"ab" 向二进制文件尾增加数据
"r+" 为读/写打开一个文本文件
"w+" 为读/写建立一个新的文本文件
"a+" 为读/写打开一个文本文件
"rb+" 为读/写打开一个二进制文本
"wb+" 为读/写建立一个新的二进制文件
"ab+" 为读/写打开一个二进制文件
文件的关闭(fclose函数)
fclose(文件指针)
文件的读写
1.字符读写函数
fputc函数的功能:把一个字符写入指定的文件中 字符输出
调用形式:fputc(ch,fp) /*将ch中国的字符写入到fp锁指向的文件中*/
fgetc函数的功能:从指定的文件读取一个字符到内存 字符输入
调用形式:fgetc(fp)
/*
在文件wm.txt中存放了若干个字符串,要求编写程序统计出其中大写字母的个数
*/
#include<stdio.h>
void main()
{
FILE *fp;
int count=0;
char c;
fp=fopen("d:\\file\\wm.txt","r");
c=fgetc(fp);
while(c!=EOF)
{
if(c>='A' && c<='Z')
count++;
c=fgetc(fp);
}
printf("the count is %d",count);
fclose(fp);
}
2,字符串读写函数
fputs函数的功能:把字符串写入到指定的文件中 字符串输出
调用形式:fputs(str,fp) /*将str开始的字符串写入到fp所指向的文件中*/
fgets函数的功能:从指定的文件读一个字符串
调用形式:fgets(str,n,fp) /*从fp所指向的文件中读出n-1个字符构成的字符串送到从str开始的内存单元中,若成功返回str的地址*/
//在显示器和文件中同时显示文本文件wm1的部分内容
#include<stdio.h>
void main()
{
FILE *fp1,*fp2;
char str[20];
fp1=fopen("d:\\wy.txt","r");
fp2=fopen("d:\\wy1.txt","w");
fgets(str,12,fp1); //从wy.txt文件中读取11个字符到str开始的地址中
fputs(str,fp2); //将str开始的字符串写入到fp2所指向的文件中
puts(str); //在显示器上显示文件wy1.txt相同的内容
fclose(fp1);
fclose(fp2);
}
3.数据块读写函数
fread(buffer,size,count,fp) /*从fp指向的文件读取count个size个字节的数据,存储到buffer开始的地址中*/
//从磁盘文件中,读入40个学生的数
for(i=0;i<40;i++) fwrite(&stud[i],sizeof(struct student_type),1,fp);
fwrite(buffer,size,count,fp)//将内存中的学生数据输出到磁盘文件中 for(i=0;i<40;i++) fwrite(&stud[i],sizeof(struct student_type),1,fp);/* 设职工数据为:工号,姓名,性别,年龄,工资,将6名职工的数据从键盘输入 然后送入磁盘文件worker.txt中保存,再读此文件并输出这些数据,依次打印出来 */ #include<stdio.h> #define SIZE 6 struct woker_type { int num; int age; char name[10]; char sex; float pay; }worker[SIZE]; void save() { FILE *fp; int i; if((fp=fopen("d:\\file\worker.txt","rb"))==NULL) { printf("can not open the file!\n"); return; } for(i=0;i<SIZE;i++) { if(fwrite(&worker[i],sizeof(struct worker_type),1,fp)!=1) printf("file write error!\n"); } fclose(fp); } void main() { int i; FILE *fp; for(i=0;i<SIZE;i++) scanf("%d %d %c %d %f",&worker[i].num,&worker[i].name,&worker[i].sex,&worker[i].age,&worker[i].pay); save(); printf("\n No Name Sex Age Pay\n"); fp=fopen("d:\\file\\worker.txt","rb"); for(i=0;i<SIZE;i++) { fread(&worker[i],sizeof(struct worker_type worker[SIZE]),1,fp); printf("%5d %-8s %-5c %-5d %6.2f\n",worker[i].num,worker[i].name,worker[i].sex,worker[i].age,worker[i].pay); } }
4.格式化读写函数fprintf函数的功能:将任意类型的数据输出到文件中 格式化输出
调用形式 : fprintf(文件指针,"格式控制字符串",输出列表); /*将输出表列中的数据按照指定格式要求存入到文件指针所指向的文件中*/
fscanf函数的功能:从文件读取任何类型的数据
调用形式:fscanf(文件指针,"格式控制字符串",地址列表) /* 按照格式要求从文件指针所指向的文件中读取数据到指定的内存地址中*/
/* 在a1.txt文件存放了100个整数,要求编写程序,程序的功能是 将这100个数按升序排列,将排列结果保存到a2.txt中 */ #include<stdio.h> void main() { FILE *fp1,*fp2; int a[100],i,j; fp1=fopen("d:\\file\\a1.txt","w"); fp2=fopen("d:\\file\\a2.txt","w"); //输入 for(i=0;i<100;i++) fscanf(fp1,"%d",&a[i]); for(i=0;i<99;i++) //冒泡排序法 for(j=0;j<99-i;j++) if(a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } //输出 for(i=0;i<100;i++) fprintf(fp2,"%d",a[i]); fclose(fp1); fclose(fp2); }
文件的定位rewind函数的作用是使位置指针重新返回文件的开头,函数没有返回值
#include<stdio.h> void main() { FILE *fp1,*fp2; fp1=fopen("d:\\file\\wm.txt","r"); fp2=fopen("d:\\file\\new.txt","w"); if(!feof(fp1)) putchar(getc(fp1)); //输出 rewind(fp1); if(!feof(fp1)) putc(getc(fp1),fp2); fclose(fp1); fclose(fp2); }
fseek函数和随机读写
fseek函数可以实现改变文件的位置指针
fseek(文件指针类型,位移量,起始点)
起始点用0,1,2代替
0代表文件开始
1代表当前位置
2代表文件末尾
/* 在磁盘文件上存有10个学生的数据,要求将第1,3,5,7,9个学生数据输入计算机 ,并在屏幕上显示出来 */ #include<stdio.h> #include<stdlib.h> struct student { char name[10]; int num; int age; char sex; }stu[10]; void main() { int i; FILE *fp; if((fp=fopen("d:\\file\\student.txt","rb+"))==NULL) { printf("can not open the file\n"); return; } for(i=0;i<10;i=i+2) //注意i的增量 i+=2 { fseek(fp,i*sizeof(struct student),0); fread(&stu[i],sizeof(struct student),1,fp); printf("%s %d %d %c\n",stu[i].name,stu[i].num,stu[i].age,stu[i].sex); } fclose(fp); }
//有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学号,姓名,三门课的成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stu中 #include<stdio.h> struct student { char num[10]; char name[8]; int score[3]; float aver; }stu[5]; void main() { int i,j,sum; FILE *fp; for(i=0;i<5;i++) { printf("\nInput score of student %d:\n",i+1); printf("NO.:"); scanf("%s",&stu[i].num); printf("name:"); scanf("%s",&stu[i].name); for(j=0;j<3;j++) { printf("score:%d",j+1); scanf("%d",&stu[i].score[j]); sum+=stu[i].score[j]; } stu[i].aver=sum/3.0; } fp=fopen("d:\\file\\stu.txt","w"); for(i=0;i<5;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File write error\n"); fclose(fp); fp=fopen("d:\\file\\stu.txt","w"); for(i=0;i<5;i++) if(fread(&stu[i],sizeof(struct student),1,fp)!=1) printf("%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); }