//typedef
typedef struct { short level; //缓冲区"满"或"空"的程度 unsigned flags;//文件状态标志 char fd;//文件描述符 unsigned char hold;//如无缓冲区不读取字符 short bsize;//缓冲区的大小 unsigned char *buffer;//数据缓冲区的位置 unsigned ar *cur;//指针当前的指向 unsigned istemp;//临时文件,指示器 short token;//用于有效性检查 }FILE;
//fopen FILE *fp; fp=fopen(文件名,使用文件方式);
//fclose fclose(文件指针);
//fputc fputc(ch,fp);//将字符(ch的值)输出到fp所指向的文件中去
//fgetc ch=fgetc(fp);//从指定的文件fp读入一个字符ch ch=fgetc(fp); /*while(ch!=EOF) { putchar(ch); ch=fgetc(fp); }*/
//feof while(!feof(fp)) { c=fgetc(fp); . . . }//当文件没有结束时,feof(fp)的值为0, !feof(fp)为1
//fputc and fgetc 函数的举例
#include(stdio.h> main() { FILE *fp; char ch,filename[10]; scanf("%s",filename); if((fp=fopen(filename,"w"))==NULL) { printf("cannot open file/n"); exit(0); } ch=getchar(); ch=getchar(); while(ch!='#') { fputc(ch,fp); putchar(ch); ch=getchar(fp); } fclose(fp); } file1.c//输入磁盘文件名 computer and c#//输入一个字符串 computer and c//输出一个字符串
//DOS命令验证的格式 c>type file1.c computer and c c>type file2.c computer and c
#include<stdio.h> main(argc,char *argv[]) { FILE *in,*out; char ch; if(argc!j=3) { printf("You forget to enter afilename/n"); exit(0); } if((in=fopen(argv[1],"r"))==NULL) { printf("cannot open infile/n"); exit(0); } if(out=fopen(argv[2],"w"))=NULL) { printf("cannot open outfile/n"); exit(0); } while(!feof(in)) fputc(fgetc(in),out); fclose(in); fclose(out); }//argc的值为3,代表有三个文件(in ,out ,原文件)
//fread fread(buffer,size,count,fp); fwrite(buffer,size,count,fp); /*buffer:是一个指针,对于fread来说,是读入数据的存放地址,对fwrite来说,是要输出数据的地址 size:要读写的字节数 count:要读写多少个size字节的数据项 fp:文件型指针*/
//fprinf and fscanf fprintf(文件指针,格式字符串,输出列表); fscanf(文件指针,格式字符串,输入列表); //egg举例: fprintf(fp,"%d,%6.3f",i,t); fscanf(fp,"%d,%6.3f",&i,&t);
//putw putw(int i,FILE*fp) { char *s; s=&i; putc(s[0],fp); putc(s[i],fp); return(i); } //getw getw(FILE *fp) { char *s; int i; s=char *&i; s[0]=getc(fp); s[1]=getc(fp); return(i); }//i占两个字节,是整型.
//定义一个向磁盘文件写一个实数的函数 putfloat(float num,FILE *fp) { char *s; int count; s=(char *)# for(count=0;count<4;count++) putc(s[count],fp); }
//fgets and fputs
fgets(str,n,fp); fputs("china",fp);
//rewind #include<stdio.h> main() { FILE *fp1,*fp2; fp1=fopen(file1.c","r"); fp2=fopen(file2.c","w"); while(!feof(fp1)); putchar(getc(fp1)); rewind(fp1); while(!feof(fp1)) putc(getc(fp1),fp2); fclose(fp1); fclose(fp2); }//使位置指针重新返回文件的开头
//fseek fseek(文件类型指针,位移量,起始点); fseek(fp,100L,0)//将位置指针移到离文件头100个字节处 fseek(fp,50L,1)//将位置指针移到离当前位置50个字节处 fseek(fp,-10L,2)//将位置指针向后退10个字节
//ftell