Question

The code currently does this and the fgetpos does handle files larger than 4GB but the seek returns an error, so any idea how to seek to the end of a file > 4GB?

fpos_t currentpos;

sok=fseek(fp,0,SEEK_END);
assert(sok==0,"Seek error!");

fgetpos(fp,&currentpos);
m_filesize=currentpos;
Was it helpful?

Solution

If you're in Windows, you want GetFileSizeEx (MSDN). The return value is a 64bit int.

On linux stat64 (manpage) is correct. fstat if you're working with a FILE*.

OTHER TIPS

Ignore all the answers with "64" appearing in them. On Linux, you should add -D_FILE_OFFSET_BITS=64 to your CFLAGS and use the fseeko and ftello functions which take/return off_t values instead of long. These are not part of C but POSIX. Other (non-Linux) POSIX systems may need different options to ensure that off_t is 64-bit; check your documentation.

This code works for me in Linux:

int64_t bigFileSize(const char *path)
{
    struct stat64 S;

    if(-1 == stat64(path, &S))
    {
        printf("Error!\r\n");
        return -1;
    }

    return S.st_size;
}

(stolen from the glibc manual)

int fgetpos64 (FILE *stream, fpos64_t *position)

This function is similar to fgetpos but the file position is returned in a variable of type fpos64_t to which position points.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fgetpos and so transparently replaces the old interface.

Stat is always better than fseek to determine file size, it will fail safely on things that aren't a file. 64 bit filesizes is an operating specific thing, on gcc you can put "64" on the end of the commands, or you can force it to make all the standard calls 64 by default. See your compiler manual for details.

On linux, at least, you could use lseek64 instead of fseek.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top