_ftruncate64 on large files?

Started by drhoule, September 24, 2013, 08:15:27 PM

Previous topic - Next topic

drhoule

I am dealing with long files...
I can use _ftruncate on short files but not long ones.

_ftruncate uses a long int.

a long long int is required.

How can I code / use _ftruncate with long files?

Any help would be nice.

Thank you in advance!
Dr.

frankie

Dindn't understand the question.
_ftruncate uses an _off_t type that is a long long.
The function doesn't care of the size of the file. You can use it on short and large files.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

TimoVJL

#2
Quote from: frankie on September 24, 2013, 10:23:37 PM
_ftruncate uses an _off_t type that is a long long.
just in x64

sys\types.h
#if __POCC_TARGET__ == 3
typedef long long _off_t;
#else
typedef long _off_t;
May the source be with you

drhoule

More info,

MS VC has this function ...
     errno_t   _chsize_s(  int fd, __int64 size );
                  but it is not implemented with Pelles C only int _chsize(int handle, long int size);

Dr

frankie

Try this:

#include <stdio.h>
#include <io.h>

BOOL SetFileLength(int fd, long long pos)
{
HANDLE fh = (HANDLE)_get_osfhandle(fd);
if (!SetFilePointerEx(fh, *((LARGE_INTEGER *)&pos), NULL, FILE_BEGIN))
return FALSE;

return SetEndOfFile(fh);
}

BOOL fset_file_len(FILE *fp, long long pos)
{
return SetFileLength(_fileno(fp), pos);
}

I had no time to test, but should work.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

drhoule

Your solution looks good.

I accept this as a reasonable fix.

I will test it and or modify it as needed, in the comming months as I work on this small project.

Thank you.

drhoule

Tested. Confirmed!

Your proposed solution is good.
Thank you very much.

Dr

frankie

"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

zohanx747

I will test it and or modify it as needed, in the comming months as I work on this small project.