Pelles C forum

C language => Beginner questions => Topic started by: drhoule on September 24, 2013, 08:15:27 PM

Title: _ftruncate64 on large files?
Post by: drhoule on September 24, 2013, 08:15:27 PM
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.
Title: Re: _ftruncate64 on large files?
Post by: frankie on September 24, 2013, 10:23:37 PM
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.
Title: Re: _ftruncate64 on large files?
Post by: TimoVJL on September 25, 2013, 05:57:42 AM
_ftruncate uses an _off_t type that is a long long.
just in x64

sys\types.h
Code: [Select]
#if __POCC_TARGET__ == 3
typedef long long _off_t;
#else
typedef long _off_t;
Title: Re: _ftruncate64 on large files?
Post by: drhoule on September 25, 2013, 12:31:29 PM
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
Title: Re: _ftruncate64 on large files?
Post by: frankie on September 25, 2013, 01:18:13 PM
Try this:
 
Code: [Select]
#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.
Title: Re: _ftruncate64 on large files?
Post by: drhoule on September 25, 2013, 02:18:24 PM
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.
Title: Re: _ftruncate64 on large files?
Post by: drhoule on September 25, 2013, 06:39:23 PM
Tested. Confirmed!

Your proposed solution is good.
Thank you very much.

Dr
Title: Re: _ftruncate64 on large files?
Post by: frankie on September 26, 2013, 03:01:02 PM
Good  8)
You're welcome
Title: Re: _ftruncate64 on large files?
Post by: zohanx747 on October 30, 2013, 11:24:39 AM
I will test it and or modify it as needed, in the comming months as I work on this small project.