fdopen:
|
fdopen(将文件描述词转为文件指针) |
相关函数
|
fopen,open,fclose |
表头文件
|
#include<stdio.h> |
定义函数
|
FILE * fdopen(int fildes,const char * mode); |
函数说明
|
fdopen()会将参数fildes 的文件描述词,转换为对应的文件指针后返回。参数mode 字符串则代表着文件指针的流形态,此形态必须和原先文件描述词读写模式相同。关于mode 字符串格式请参考fopen()。 |
返回值
|
转换成功时返回指向该流的文件指针。失败则返回NULL,并把错误代码存在errno中。 |
范例
|
#include<stdio.h> main() { FILE * fp =fdopen(0,”w+”); fprintf(fp,”%s\n”,”hello!”); fclose(fp); } |
执行
|
hello! |
freopen:
|
freopen(打开文件) |
相关函数
|
fopen,fclose |
表头文件
|
#include<stdio.h> |
定义函数
|
FILE * freopen(const char * path,const char * mode,FILE * stream); |
函数说明
|
参数path字符串包含欲打开的文件路径及文件名,参数mode请参考fopen()说明。参数stream为已打开的文件指针。Freopen()会将原stream所打开的文件流关闭,然后打开参数path的文件。 |
返回值
|
文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno 中。 |
范例
|
#include<stdio.h> main() { FILE * fp; fp=fopen(“/etc/passwd”,”r”); fp=freopen(“/etc/group”,”r”,fp); fclose(fp); } |
fileno:
|
fileno(返回文件流所使用的文件描述词) |
相关函数
|
open,fopen |
表头文件
|
#include<stdio.h> |
定义函数
|
int fileno(FILE * stream); |
函数说明
|
fileno()用来取得参数stream指定的文件流所使用的文件描述词。 |
返回值
|
返回文件描述词。 |
范例
|
#include<stdio.h> main() { FILE * fp; int fd; fp=fopen(“/etc/passwd”,”r”); fd=fileno(fp); printf(“fd=%d\n”,fd); fclose(fp); } |
执行
|
fd=3 |
/*
============================================================================
Name : CDX.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(void) {
FILE *fp=NULL;
//重定向cat main.c>txt:将mian,c里面的内容输入到txt里面去
fp=fopen("hello.txt","w");
if(fp==NULL)
{
perror("open error");
return 0;
}
// fp=freopen(“/etc/group”,”r”,fp);
freopen("world.txt","w+",fp);
fputs("helloweotd",fp);
//fieno
int fd;
fd=fileno(fp);
printf("fd=%d\n",fd);
fclose(fp);
FILE * fp1 =fdopen(0,"w+");
fprintf(fp1,"%s\n","hello!");
//puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
ftell:
|
ftell(取得文件流的读取位置) |
相关函数
|
fseek,rewind,fgetpos,fsetpos |
表头文件
|
#include<stdio.h> |
定义函数
|
long ftell(FILE * stream); |
函数说明
|
ftell()用来取得文件流目前的读写位置。参数stream为已打开的文件指针。 |
返回值
|
当调用成功时则返回目前的读写位置,若有错误则返回-1,errno会存放错误代码。 |
错误代码
|
EBADF 参数stream无效或可移动读写位置的文件流。 |
范例
|
参考fseek()。 |
|
fseek(移动文件流的读写位置) |
相关函数
|
rewind,ftell,fgetpos,fsetpos,lseek |
表头文件
|
#include<stdio.h> |
定义函数
|
int fseek(FILE * stream,long offset,int whence); |
函数说明
|
fseek()用来移动文件流的读写位置。参数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会存放错误代码。 |
附加说明
|
fseek()不像lseek()会返回读写位置,因此必须使用ftell()来取得目前读写的位置。 |
|
rewind(重设文件流的读写位置为文件开头) |
相关函数
|
fseek,ftell,fgetpos,fsetpos |
表头文件
|
#include<stdio.h> |
定义函数
|
void rewind(FILE * stream); |
函数说明
|
rewind()用来把文件流的读写位置移至文件开头。参数stream为已打开的文件指针。此函数相当于调用fseek(stream,0,SEEK_SET)。 |
返回值
|
|
范例
|
参考fseek() |
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char **argv) {
//函数:fseek(file *stream,long off_set,int wherece)可以使文件指针移到文件中的指定位置
//long ftell(file *stream,fpos_t *pos);
FILE *fp;
int i;
char buf[9][10];
fp=fopen("/home/wzm/workspace1/filesuiji/src/tab.dat","r");
if(fp==NULL)
{
perror("open error");
return -1;
}
//把文件指针移动30个位置
fseek(fp,30,SEEK_SET);
//输出当前的指针位置
printf("current file position[%ld]\n",ftell(fp));
for(i=0;i<6;i+=3)
{
fscanf(fp,"%10s%10s%10s\n",buf[i],buf[i+1],buf[i+2]);
printf(fp,"%s%s%s\n",buf[i],buf[i+1],buf[i+2]);
}
fclose(fp);
return EXIT_SUCCESS;
}