目录
1.setvbuf
功能:
把缓冲区与流相关 setbuf()和setvbuf()函数的实际意义在于:用户打开一个文件后,可以建立自己的文件缓冲区,而不必使用fopen()函数打开文件时设定的默认缓冲区。这样就可以让用户自己来控制缓冲区,包括改变缓冲区大小、定时刷新缓冲区、改变缓冲区类型、删除流中默认的缓冲区、为不带缓冲区的流开辟缓冲区等。 |
函数原型:
int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
用 法:
mode : 期望缓冲区的类型:
_IOFBF(满缓冲):当缓冲区为空时,从流读入数据。或者当缓冲区满时,向流写入数 据。
_IOLBF(行缓冲):每次从流中读入一行数据或向流中写入一行数据。
_IONBF(无缓冲):直接从流中读入数据或直接向流中写入数据,而没有缓冲区。
size : 缓冲区内字节的数量。
注意:This function should be called once the file associated with the stream has already been opened but before any input or output operation has taken place.
意思是这个函数应该在打开流后,立即调用,在任何对该流做输入输出前
例子演示:
#include <stdio.h>
#include "stdlib.h"
int main()
{
FILE *input;
FILE *output;
char bufr[512];
input = fopen("file.in", "w+"); /*打开文件*/
if (input==NULL)
{
perror("file.in");
exit(EXIT_FAILURE);
}
output = fopen("file.out", "w");
if (output == NULL)
{
perror("file.out");
exit(EXIT_FAILURE);
}
if (setvbuf(input, bufr, _IOFBF, 512) != 0) /*失败*/
{
printf("failed to set up buffer for input file\n");
}
else
{
printf("buffer set up for input file\n");
}
if (setvbuf(output, NULL, _IOLBF, 132) != 0) /*为流指定特殊的缓冲区*/
{
printf("failed to set up buffer for output file\n");
}
else
{
printf("buffer set up for output file\n");
}
fclose(input);
input = NULL;
fclose(output);
output = NULL;
return 0;
}
2.rewind
函数名: rewind()
功 能: 将文件内部的位置指针重新指向一个流(数据流/文件)的开头
注意:不是文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动。而文件指针是指向整个文件,如果不重新赋值文件指针不会改变。
文件指针FILE *fp中,包含一个读写位置指针char *_nextc,它指向下一次文件读写的位置。
typedef struct
{
int _fd; /* 文件号 */
int _cleft; /* 缓冲区中剩下的字节数 */
int _mode; /* 文件操作模式 */
char * _nextc; /* 下一个字节的位置 */
char * _buff; /*文件缓冲区位置 */
}FILE;
每当进行一次读写后,该指针自动指向下一次读写的位置。
当文件刚打开或创建时,该指针指向文件的开始位置。
可以用函数ftell()获得当前的位置指针,也可以用rewind()/fseek()函数改变位置指针,使其指向需要读写的位置。
rewind函数作用等同于 (void)fseek(stream, 0L, SEEK_SET);
用 法: void rewind(FILE *stream);
頭文件: stdio.h
返回值:无
程序例子:
#define _CRT_SECURE_NO_DEPRECATE 1
#include "stdio.h"
#include"stdlib.h"
int main()
{
int len = 0;
char *p = "hello world";
FILE* pf = NULL;
pf = fopen("rewind.test", "w+");
if (NULL==pf)
{
perror("rewind.test");
exit(EXIT_FAILURE);
}
fputs(p,pf);
/*fwrite(p, sizeof(char), 11, pf);*/
len=ftell(pf);
printf("%d\n", len);
rewind(pf);
len = ftell(pf);
printf("%d\n", len);
fclose(pf);
pf = NULL;
return 0;
}
3.fgetpos
功 能: 取得当前文件的句柄
用法:int fgetpos( FILE *stream, fpos_t *pos );
例子:
#include <string.h>
#include <stdio.h>
#include "stdlib.h"
int main()
{
FILE *stream;
fpos_t filepos;
char string[] = "This is a test";
stream = fopen("file.txt", "w+");
if (NULL==stream)
{
perror("file.txt");
exit(EXIT_FAILURE);
}
fwrite(string, strlen(string), 1, stream);
fgetpos(stream, &filepos);
printf("The file pointer is at byte:%ld\n", filepos);
fclose(stream);
stream = NULL;
return 0;
}
4.fsetpos
函数功能:将文件指针定位在pos指定的位置上。该函数的功能与前面提到的fgetpos相反,是将文件指针fp按照pos指定的位置在文件中定位。pos值以内部格式存储,仅由fgetpos和fsetpos使用。
用 法: int fsetpos(FILE *stream, const fpos_t *pos);
例子:
#include <stdio.h>
#include "stdlib.h"
int main(void)
{
FILE *fp;
fpos_t position;
char arr[50];
fp = fopen("file.txt", "w+");
if (NULL == fp)
{
perror("file.txt");
exit(EXIT_FAILURE);
}
fgetpos(fp, &position);
fputs("Hello, World!", fp);
fsetpos(fp, &position);
fputs("This is going to override previous content", fp);
fseek(fp, 0, SEEK_SET);
fgets(arr,50,fp);
printf("%s\n", arr);
fclose(fp);
fp = NULL;
return 0;
}
5.feof
功能:检测流上的文件结束符,既可用以判断二进制文件又可用以判断文本文件。
用法:int feof(FILE *stream);
例子:
#include "stdio.h"
#include "stdlib.h"
int main()
{
FILE* pf = NULL;
int ret = 0;
pf = fopen("file.tex", "w+");
if (NULL == pf)
{
perror("file.txt");
exit(EXIT_FAILURE);
}
fputs("hello", pf);
fseek(pf, 0, SEEK_END);
fgetc(pf);
ret = feof(pf);
if (ret)
{
printf("文件結束\n");
}
fclose(pf);
pf = NULL;
return 0;
}