【www.gdgbn.com--php函数】

file *fopen(const char *filename, const char *mode)
filename 打开的文件名 mode 打开模式 返回文件指针

 

size_t fread(void *ptr, size_t size, size_t, file *stream)
从stream指向的文件中读取n个字段,每个字段为size字节,并将读取的数据放在ptr所指的字符数组中,返回实际读取字节数

 

size_t fwrite(const void *ptr, size_t size, size_t n, file *stream)
从缓冲区ptr所指的数组中把n个字段写到stream指向的文件中,每个字段长为size个字节,返回实际写入的字段数。
int fgetc(file *stream)
从指定文件中读取一个字符
例子 1:

 

#include 
#include
int main(void)
{
file *fp;
char ch;
if((fp = fopen("test.txt","rt")) == null)
{
printf("file open error!");
exit(1);
}
ch = fgetc(fp);
while(ch != eof)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
}

 

int fputc(int c, file *stream)
向指定的文件中写入一个字符
例子 2:
#include 
#include
int main(void)
{
file *fp;
char ch;
if((fp = fopen("test.txt","wt+")) == null)
{
printf("file open error!");
exit(1);
}
printf("please enter a stringn");
ch = getchar();
while(ch != "n")
{
fputc(ch, fp);
ch = getchar();
}
fclose(fp);
}


fscanf(file *stream, char *format [,argument...])
从一个流中进行格式化输入
#include 
#include
int main(void)
{
int i;
printf("please enter an interger:");
if(fscanf(stdin, "%d",&i)) //从标准输入流读取
printf("the interger is :%dn",i);
return 0;
}

int fprintf(file *stream, char *format[,argument...])
格式化输出到一个流中
#include 
#include
int main(void)
{
file *stream;
int i = 10;
double fp = 1.5;
char s[] = "hello world";
char c = "n";
stream = fopen("fprint.out", "w");
fprintf(stream, "%s%c", s, c);
fprintf(stream, "%dn", i);
fprintf(stream, "%fn", fp);
fclose(stream);
return 0;
}

int fseek(file *stream, long offset, int whence)
设置文件指针whence : seek_set:从文件的开始处开始搜索 seek_cur:从当前位置开始搜索 seek_end:从文件结尾处开始搜索 <script type=text/网页特效> if ($ != jquery) { $ = jquery.noconflict(); } var islogined = false; var cb_blogid = 76592; var cb_entryid = 2015515; var cb_blogapp = "lovetong"; var cb_bloguserguid = "7403e7b6-b0cd-df11-ac81-842b2b196315"; var cb_entrycreateddate = "2011/4/14 0:05:00"; </script>

本文来源:http://www.gdgbn.com/jiaocheng/29890/