文件操作函数总结
1.fopen( )
2.fclose( )
3.fread( )
4.fwrite( )
5.fseek( )
6.fprintf( )
7.fscanf( )
8.ftell( )
1.fopen( )
fopen(打开文件) 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代表着流形态。 mode有下列几种形态字符串: r 打开只读文件,该文件必须存在。 r+ 打开可读写的文件,该文件必须存在。 w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。 w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。 a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX系统,包含Linux都会忽略该字符。由fopen()所建立的新文件会具有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件权限也会参考umask 值。 返回值 文件顺利打开后,指向该流的文件指针就会被返回。若果文件打开失败则返回NULL,并把错误代码存在errno 中。 附加说明 一般而言,开文件后会作一些文件读取或写入的动作,若开文件失败,接下来的读写动作也无法顺利进行,所以在fopen()后请作错误判断及处理。 【例程】 #include <stdio.h> int main() { FILE *fp1; char msg[] = "this is a test too"; fp1=fopen("/home/duodata/yali.c","w+"); fwrite(msg, strlen(msg)+1, 1, fp1); return 0; }
2.fclose( )
函数名: fclose 功 能: 关闭一个流 用 法: int fclose(FILE *stream); 程序例: #include <string.h> #include <stdio.h> int main(void) { FILE *fp; char buf[11] = "0123456789"; /* create a file containing 10 bytes */ fp = fopen("DUMMY.FIL", "w"); fwrite(&buf, strlen(buf), 1, fp); /* close the file */ fclose(fp); return 0; } 如果流成功关闭,fclose 返回 0,否则返回EOF(-1)。 如果流为NULL,而且程序可以继续执行,fclose设定error number给EINVAL,并返回EOF。
3.fread( )
函数名: fread 功 能: 从一个流中读数据 用 法: int fread(void *ptr, int size, int nitems, FILE *stream); 参 数:用于接收数据的地址(字符型指针)(ptr) 单个元素的大小(size) 元素个数(nitems) 提供数据的文件指针(stream) 返回值:成功读取的元素个数 程序例: #include <string.h> #include <stdio.h> int main(void) { FILE *stream; char msg[] = "this is a test"; char buf[20]; if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output file./n"); return 1; } /* write some data to the file */ fwrite(msg, strlen(msg)+1, 1, stream); /* seek to the beginning of the file */ fseek(stream, 0, SEEK_SET); /* read the data and display it */ fread(buf, 1, strlen(msg)+1, stream); printf("%s/n", buf); fclose(stream); return 0; }
3.fwrite( )
函数名: fwrite 功 能: 写内容到流中 用 法:fwrite(buffer,size,count,fp); (1)buffer:是一个指针,对fwrite来说,是要输出数据的地址。 (2)size:要写入的字节数; (3)count:要进行写入size字节的数据项的个数; (4)fp:目标文件指针。 说明:写入到文件的哪里?这个与文件的打开模式有关,如果是r+,则是从file pointer指向的地址开始写,替换掉之后的内容,文件的长度可以不变;如果是a+,则从文件的末尾开始添加,文件长度加大,而且是fseek函数对此函数没有作用。 程序例: #include <stdio.h> struct mystruct { int i; char ch; }; int main(void) { FILE *stream; struct mystruct s; if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */ { fprintf(stderr, "Cannot open output file./n"); return 1; } s.i = 0; s.ch = 'A'; fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */ fclose(stream); /* close file */ return 0; }
4.fseek( )
函数名: fseek
功 能: 重定位流上的文件指针
用 法: int fseek(FILE *stream, long offset, int fromwhere);
描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
返回值: 成功,返回0,否则返回其他值。
程序例:
#include <stdio.h>
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes/n", filesize(stream));
fclose(stream);
return 0;
}
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
int fseek( FILE *stream, long offset, int origin );
第一个参数stream为文件指针
第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
SEEK_SET: 文件开头
其中SEEK_CUR,SEEK_END和SEEK_SET依次为1,2和0
5.ftell( )
函数名: ftell 功 能: 返回当前文件指针(offset) 用 法: long ftell(FILE *stream); 程序例: #include <stdio.h> int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("The file pointer is at byte / %ld/n", ftell(stream)); fclose(stream); return 0; } 这个函数其实就是返回 当前 文件的指针 所在文件中的位置,也就是说,文件指针在文件中读数据时目前已经读到哪里了,返回这个位置,以字节为单位,通常跟fseek()一起使用,作为他的offset那个参数的值.ftell一般用于读取文件的长度,就像前面所说,一般与fseek()文件定位函数一起用 示例: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp; int flen; char *p; fp = fopen ("1.txt","rb"); if(fp==NULL) { return 0; } fseek(fp,0L,SEEK_END);//定位到文件末尾 flen=ftell(fp); //求文件大小 p=(char *)malloc(flen+1); //分配文件大小那么大的内存 if(p==NULL) { fclose(fp); return 0; } fseek(fp,0L,SEEK_SET); //定位到文件头 fread(p,flen,1,fp); //一次性读取文件 p[flen]=0; //把结尾清0 printf("%s/n",p); fclose(fp); free(p); return 0; }
6.fprintf( )
函数名: fprintf 功 能: 传送格式化输出到一个流中 用 法: int fprintf(FILE *stream, char *format[, argument,...]); 返回值:成功时返回转换的字节数,失败时返回一个负数 示例1: #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> main() { int n; n=fprintf(stderr,"hello/n"); printf("%d/n",n); } [root@localhost home]# ./fprintf hello 6 /n算做一个字符。linux系统默认打开标准输入、标准输出、标准错误输出三个文件,给出3个句柄stdin、stdout、stderr,信息最后出现在终端内。 示例2: #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> main() {int n,m=2; FILE *fp; if((fp=fopen("/home/yali.c","w"))!=NULL) printf("create yali.c/r/n"); else printf("Cannot create yali.c/r/n"); n=fprintf(fp,"%d",m); printf("%d/n",n); } [root@localhost home]# ./fprintf create yali.c 1 yali.c的内容为2. 示例3: #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> main() {
int n,m=2; n=fprintf(stderr,"%d/n",m); printf("%d/n",n); } [root@localhost home]# ./fprintf 2 2 同样,/n算做一个字符。
7.fscanf( )
函数名: fscanf 功 能: 从一个流中执行格式化输入(比如,可以从文件中读取需要的数据) 用 法: int fscanf(FILE *stream, char *format[,argument...]); 示例: #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> main() {int m=2,k=0,l=0; FILE *fp; fp=fopen("/home/yali.c","r+"); l=fscanf(fp,"%d",&k); printf("fscanf return value is l=%d/n",l); printf("fscanf content is k=%d,this is the content in yali.c/n",k); } [root@localhost home]# ./fscanf fscanf return value is l=1 fscanf content is k=6,this is the content in yali.c
8.ftell()
函数名: ftell
功 能: 返回当前文件指针
用 法: long ftell(FILE *stream);
程序例:
#include <stdio.h>
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("The file pointer is at byte /
%ld/n", ftell(stream));
fclose(stream);
return 0;
}
ftell()函数用于获取文件位置指针。
函数 ftell() 用于得到文件当前位置相对于文件首的偏移字节数。在随机方式存取文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。调用函数ftell()就能非常容易地确定文件的当前位置。
函数 ftell() 的调用形式为
ftell(fp);利用函数 ftell() 也能方便地知道一个文件的长。如以下语句序列: fseek(fp, 0L,SEEK_END); len =ftell(fp); 首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相对于文件首的位移,该位移值等于文件所含字节数。
