fseek
根据文件指针的位置和偏移量来定位文件指针。
int fseek ( FILE * stream, long int offset, int origin );
例子:
int main(){
FILE* pFile;
pFile = fopen("d:/text1.txt", "wb");//wb打开二进制文件,只写
fputs("hello world", pFile);
fseek(pFile, 6, SEEK_SET);
fputs(" dlrow", pFile);
fclose(pFile);
system("pause");
return 0;
}
打开文本

ftell
返回文件指针相对于起始位置的偏移量
例子:
int main(){
FILE * pFile;
long size;
pFile = fopen("d:/text1.txt", "rb");//rb只读
if (pFile == NULL){
perror("打开文件失败");
}
else
{
fseek(pFile, 0, SEEK_END);// non-portable
size = ftell(pFile);
fclose(pFile);
printf("Size of text1.txt: %ld bytes.\n", size);
}
return 0;
}
本文详细介绍了fseek和ftell两个C标准库函数的使用方法。fseek函数允许程序员根据文件指针的位置和偏移量来重新定位文件指针,通过示例展示了如何使用fseek函数修改文件内容。ftell函数则用于获取文件指针相对于起始位置的偏移量,文章通过一个示例说明了如何使用ftell函数获取文件大小。
1879

被折叠的 条评论
为什么被折叠?



