Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

_lseeki64(), _ftelli64 and _fseeki64

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
My application creates a large file and I am trying to use _lseeki64() to position the file pointer. I have found that after a write I need to call fflush() to make it work correctly. If I call it after a read using SEEK_CUR it always positions the file relative to the end.

I have found some references on the internet to _fseeki64() and _ftelli64() but I cannot find them in any include file or library.
Code:
#include "stdafx.h"
#include <io.h>

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *fp;
	char buf[256];
	__int64 i64;

	fp = fopen("test.txt","w");
	fwrite("123456789A123456789B123456789C123456789D", 40, 1, fp);
	i64 = _lseeki64(_fileno(fp), (__int64)-10, SEEK_CUR);
	printf("Before fflush = %d\n", i64);
	fflush(fp);
	i64 = _lseeki64(_fileno(fp), (__int64)-10, SEEK_CUR);
	printf("After fflush = %d\n", i64);
//	i64 = _ftelli64(fp);
	fclose(fp);
	fp = fopen("test.txt","r");
	fread(buf, 20, 1, fp);
	i64 = _lseeki64(_fileno(fp), (__int64)-10, SEEK_CUR);
	printf("From current postion after read = %d\n", i64);
	i64 = _lseeki64(_fileno(fp), (__int64)10, SEEK_SET);
	printf("From beginning of file after read = %d\n", i64);
	fclose(fp);
	return 0;
}

Any assistance would be greatly appreciated



CaKiwi
 
Remember C file stream (default) buffering. _lseeki64 is low level non-standard routine, it does not know about stream i/o (and vice versa). Obviously, all file was in memory (in buffer) and file pointer was at EOF after the 1st fread.

Turn off buffering immediately after fopen:
Code:
setvbuf(fp,0,_IONBF,0);
You have a chance to control next i/o start point in that case. But it's a risk to mix stream and low-level i/o. Avoid this.

Apropos, add I64 prefix in format specifiers for __int64 type arg of printf (see VC help for printf format):
Code:
printf("...%I64d...",i64var);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top