文件的打開(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);
}