ファイルの基本操作


ファイルの開く(fopen関数)
FILE *fp;
fp=fopen(ファイル名、ファイルの使い方)
ファイルの使用方法の意味“r”(読み取り専用)入力のために1つのテキストファイル“w”(書き込み専用)を開いて出力のために1つのテキストファイル“a”(追加)を開いてテキストファイルの最後にデータ“rb”を増加して入力のために1つのバイナリファイル“wb”を開きます出力用にバイナリファイルabを開くバイナリファイルの最後にデータr+を追加読み書き用にテキストファイルw+を開く読み書き用に新しいテキストファイルa+を作成読み書き用にテキストファイルrb+を開く読み取り/書き込み用にバイナリテキスト「wb+」を開き、読み取り/書き込み用に新しいバイナリファイル「ab+」を作成し、読み取り/書き込み用にバイナリファイルを開きます.
ファイルのクローズ(fclose関数)
fclose(ファイルポインタ)
ファイルの読み書き
1.文字読み書き関数
fputc関数の機能:指定したファイルに1文字を書き込む文字出力
呼び出し形式:fputc(ch,fp)/*ch中国の文字をfpロックが指すファイルに書き込む*/
fgetc関数の機能:指定したファイルから1文字をメモリ文字入力に読み込む
呼び出し形式: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!
"); return; } for(i=0;i<SIZE;i++) { if(fwrite(&worker[i],sizeof(struct worker_type),1,fp)!=1) printf("file write error!
"); } 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("
No Name Sex Age Pay
"); 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
",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\
ew.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
"); 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
",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("
Input score of student %d:
",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
"); 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
",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); }