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;
}