1、fopen(打开文件)
#include
FILE * fopen(const char * path, const char * mode);
path:字符串包含欲打开的文件路径及文件名
mode:字符串代表着流形态
r: 打开只读文件,该文件必须存在
r+:打开可读写文件,该文件必须 存在
w:打开只写文件,若文件存在则文件长度清为0,即文件文件内容会消失,若文件不存在则创建该文件
w+:打开可读写文件,若文件存在则文件长度清为0,即文件文件内容会消失,若文件不存在则创建该文件
a:以附加的方式打开只写文件,若文件存在则文件长度清为0,即文件文件内容会消失,若文件不存在则创建该文件
a+:以附件方式打开可读写的文件,若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
范例:
#include
main()
{
FILE * fp;
fp=fopen(“noexist”,”a+”);
if(fp= =NULL) return;
fclose(fp);
}
#include
main()
{
FILE * fp;
fp=fopen(“noexist”,”a+”);
if(fp= =NULL) return;
fclose(fp);
}
2、fprintf(传送格式化输出到一个文件中)
#include
int fprintf(FILE *stream, char *format[,argument, ...]);
FILE *:一个FILE型的指针
char *:格式化输入函数,和printf里的格式一样
返回值:成功时返回转换的字节数,失败时返回一个负数
范例:
FILE *fp = fopen("D:\\a.c", "a+");
fprintf(fp, "%s\n", str);
3、fscanf(从一个流中执行格式化输入)
#include
int fscanf(FILE *stream, char *format[,argument,...]);
FILE *:一个FILE型的指针
char *:格式化输出函数,和scanf里的格式一样
返回值:成功时返回转换的字节数,失败时返回一个负数
范例:
FILE *fp = fopen("D:\\a.c", "a+");
fscanf(fp, "%s", str);
4、fclose(关闭文件)
相关函数:close,fflush,fopen,setbuf
#include
int fclose(FILE *stream);
函数说明:fclose()用来关闭先前fopen()打开的文件,此动作会让缓冲区的数据写入文件中,并释放系统所提供的文件资源。
返回值:若关闭文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存到errno.
5、feof(检查文件流是否读到了文件尾)
#include
int feof(FILE *stream);
函数说明:feof()用来侦测是否读取到了文件尾,尾数stream为fopen()所返回的文件指针,如果已到文件尾则返回非零值,其它情况返回0。
返回值:返回非零值代表已到达文件尾
6、fflush(更新缓冲区)
#include
int fflush(FILE *stream);
函数说明:fflush()会强迫缓冲区内的数据写回参数stream指定的文件中。如果参数stream为NULL,fflush()会将所有打开的文件数据更新。
返回值:成功返回0,失败返回EOF,错误代码在于errno中
7、fgetc(从文件中读取一个字符)
#include
int fgetc(FILE *stream);
函数说明:从参数stream所指的文件中读取一个字符。若读到文件尾而无数据时便返回EOF。
返回值:getc()会返回读取到的字符,若返回EOF则表示到了文件尾。
范例:
#include
main()
{
FILE *fp;
int c;
fp=fopen(“exist”,”r”);
while((c=fgetc(fp))!=EOF)
printf(“%c”,c);
fclose(fp);
}
main()
{
FILE *fp;
int c;
fp=fopen(“exist”,”r”);
while((c=fgetc(fp))!=EOF)
printf(“%c”,c);
fclose(fp);
}
8、fgets(由文件中读取一字符串)
#include
char * fgets(char *s, int size, FILE *stream);
函数说明:用来从参数stream所指的文件内读入字符并存到参数s所指的内存空间,直到出现换行字符,读到文件尾或是已读了size-1个字符为止,最后会加上NULL作为字符串结束。
返回值:gets()若成功则返回s指针,返回NULL则表示有错误发生。
范例:
#include
main()
{
char s[80];
fputs(fgets(s,80,stdin),stdout);
}
执行 this is a test
this is a test
main()
{
char s[80];
fputs(fgets(s,80,stdin),stdout);
}
执行 this is a test
this is a test
9、fputc(将一指定字符写入文件流中)
#include
int fputc(int c, FILE *stream);
函数说明:将参数c转为unsigned char后写入参数stream指定的文件中
返回值:返回写入成功的字符,即参数c。若返回EOF则代表写入失败
范例:
#include
main()
{
FILE * fp;
char a[26]=”abcdefghijklmnopqrstuvwxyz”;
int i;
fp= fopen(“noexist”,”w”);
for(i=0;i<26;i++)
fputc(a,fp);
fclose(fp);
}
#include
main()
{
FILE * fp;
char a[26]=”abcdefghijklmnopqrstuvwxyz”;
int i;
fp= fopen(“noexist”,”w”);
for(i=0;i<26;i++)
fputc(a,fp);
fclose(fp);
}
10、fputs(将一指定的字符串写入文件内)
#include
int fputs(const char *s, FILE * stream);
函数说明:用来将参数s所指的字符串定稿到参数stream所指的文件内
返回值:若成功则返回写出的字符个数,返回EOF则表示有错误发生
11、fread(从文件流读取数据)
#include
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
函数说明:用来从文件流中读取数据
ptr:指向欲存放读取进来的数据空间
size:
nmemb:读取的字符数
stream:已打开的文件指针
返回值:返回实际读取到的nmemb数目,如果此值比参数nmemb来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况
范例:
#include
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
int main(){
FILE * stream;
int i;
stream = fopen(“/tmp/fwrite”,”r”);
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i
printf(“name[%d]=%-20s:size[%d]=%d/n”,i,s.name,i,s.size);
}
执行
name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
int main(){
FILE * stream;
int i;
stream = fopen(“/tmp/fwrite”,”r”);
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i
printf(“name[%d]=%-20s:size[%d]=%d/n”,i,s.name,i,s.size);
}
执行
name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11
12、fseek(移动文件流到读写位置)
#include
int fseek(FILE *stream, long offset, int whence);
函数说明:用来移动文件流的读写位置
stream:已打开的文件指针
offset:根据参数whence来移动读写位置的位移数
whence:SEEK_SET:从距文件开头offset位移量为新的读写位置
SEEK_CUR:以目前的读写位置往后增加offset个位移量
SEEK_END:将读写位置指向文件尾后再增加offset个位移量
当whence值为SEEK_CUR或SEEK_END时,参数offset允许负值的出现
下列是较特别的使用方式:
1) 欲将读写位置移动到文件开头时:fseek(FILE *stream,0,SEEK_SET);
2) 欲将读写位置移动到文件尾时:fseek(FILE *stream,0,0SEEK_END);
返回值当调用成功时则返回0,若有错误则返回-1,errno会存放错误代码。
1) 欲将读写位置移动到文件开头时:fseek(FILE *stream,0,SEEK_SET);
2) 欲将读写位置移动到文件尾时:fseek(FILE *stream,0,0SEEK_END);
返回值当调用成功时则返回0,若有错误则返回-1,errno会存放错误代码。
附加说明 fseek()不像lseek()会返回读写位置,因此必须使用ftell()来取得目前读写的位置。
范例:
#include
main()
{
FILE * stream;
long offset;
fpos_t pos;
stream=fopen(“/etc/passwd”,”r”);
fseek(stream,5,SEEK_SET);
printf(“offset=%d/n”,ftell(stream));
rewind(stream);
fgetpos(stream,&pos);
printf(“offset=%d/n”,pos);
pos=10;
fsetpos(stream,&pos);
printf(“offset = %d/n”,ftell(stream));
fclose(stream);
}
执行 offset = 5
offset =0
offset=10
main()
{
FILE * stream;
long offset;
fpos_t pos;
stream=fopen(“/etc/passwd”,”r”);
fseek(stream,5,SEEK_SET);
printf(“offset=%d/n”,ftell(stream));
rewind(stream);
fgetpos(stream,&pos);
printf(“offset=%d/n”,pos);
pos=10;
fsetpos(stream,&pos);
printf(“offset = %d/n”,ftell(stream));
fclose(stream);
}
执行 offset = 5
offset =0
offset=10
13、ftell(取得文件流的读取位置)
#include
long ftell(FILE *stream);
函数说明:用来取得文件流目前的读写位置
stream:已打开的文件指针
返回值:当调用成功时则返回目前的读写位置,若有错误则返回-1,errno会存放错误代码。
14、fwrite(将数据写至文件流)
#include
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
函数说明:用来将数据写入文件流中
ptr:指向欲写入的数据地址
nmemb:总共写入的字符数
stream:已打开的文件指针
返回值:返回实际写入的nmemb数目
范例:
#include
#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
set_s(0,”Linux!”);
set_s(1,”FreeBSD!”);
set_s(2,”Windows2000.”);
stream=fopen(“/tmp/fwrite”,”w”);
fwrite(s,sizeof(struct test),nmemb,stream);
fclose(stream);
}
#include
#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
set_s(0,”Linux!”);
set_s(1,”FreeBSD!”);
set_s(2,”Windows2000.”);
stream=fopen(“/tmp/fwrite”,”w”);
fwrite(s,sizeof(struct test),nmemb,stream);
fclose(stream);
}
15、getc(由文件中读取一个字符)
#include
int getc(FILE *stream)
stream:已打开的文件
函数说明:用来从参数stream所指的文件中读取一个字符。若读到文件尾而无数据时便返回EOF。虽然getc()与fgetc()作用相同,但getc()为宏定义,非真正的函数调用。
返回值:getc()会返回读取到的字符,若返回EOF则表示到了文件尾。
16、getchar(由标准输入设备内读进一字符)
#include
int getchar(void)
函数说明:用来从标准输入设备中读取一个字符
返回值:返回读取到的字符,若返回EOF则表示有错误发生。
范例:
#include
main()
{
FILE * fp;
int c,i;
for(i=0li<5;i++)
{
c=getchar();
putchar(c);
}
}
执行 1234
1234
main()
{
FILE * fp;
int c,i;
for(i=0li<5;i++)
{
c=getchar();
putchar(c);
}
}
执行 1234
1234
17、gets(由标准输入设备内读进一字符串)
#include
char *gets(char *s);
函数说明:用来从标准设备读入字符并存到参数s所指的内存空间,直到出现换行字符或读到文件尾为止,最后加上NULL作为字符串结束。
返回值:成功则返回s指针,返回NULL则表示有错误发生
附加说明由于gets()无法知道字符串s的大小,必须遇到换行字符或文件尾才会结束输入,因此容易造成缓冲溢出的安全性问题。建议使用fgets()取代。
18、putc(将一指定字符写入文件中)
#include
int putc(int c, FILE *stream);
函数说明:将参数c转为unsigned char后写入参数stream指定的文件中。虽然putc()与fputc()作用相同,但putc()为宏定义,非真正的函数调用。
返回值 putc()会返回写入成功的字符,即参数c。若返回EOF则代表写入失败。
19、putchar(将指定的字符写到标准输出设备)
#include
int putchar(int c);
函数说明:用来将参数c字符定到标准输出设备
返回值:返回输出成功的字符,即参数c。若返回EOF则代表输出失败
附加说明 putchar()非真正函数,而是putc(c,stdout)宏定义。
20、rewind(重设文件流的读写位置为文件开头)
#include
void rewind(FILE *stream);
函数说明:用来把文件流的读写位置移至文件开头。
stream:已打开的文件指针
相当于调用fseek(stream, 0, SEEK_SET);
21、ungetc(将指定字符写回文件流中)
#include
int ungetc(int c, FILE *stream);
函数说明:将参数c字符写回参数stream所指定的文件
返回值:成功则返回c字符,若有错误则返回EOF