确定大于4GB的文件的大小

时间:2022-11-07 17:22:02

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?

目前的代码是这样做的,而fgetpos确实处理大于4GB的文件,但是search返回了一个错误,所以有什么想法要如何查找文件> 4GB的末尾吗?

fpos_t currentpos;

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

fgetpos(fp,&currentpos);
m_filesize=currentpos;

6 个解决方案

#1


5  

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

如果你在Windows中,你需要GetFileSizeEx (MSDN)。返回值是64位整数。

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

在linux stat64 (manpage)上是正确的。fstat如果您正在处理一个文件*。

#2


8  

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.

忽略所有出现“64”的答案。在Linux上,您应该向CFLAGS添加-D_FILE_OFFSET_BITS=64,并使用fseeko和ftello函数来获取/返回off_t值,而不是long。它们不是C的一部分,而是POSIX的一部分。其他(非linux) POSIX系统可能需要不同的选项来确保off_t是64位的;检查你的文档。

#3


2  

This code works for me in Linux:

这段代码适用于我在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;
}

#4


1  

(stolen from the glibc manual)

(从glibc手册偷来)

int fgetpos64 (FILE *stream, fpos64_t *position)

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.

此函数类似于fgetpos,但文件位置返回在fpos64_t类型的变量中,该变量指向哪个位置。

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.

如果源是在32位机器上使用_FILE_OFFSET_BITS == 64来编译的,这个函数可以在fgetpos的名称下使用,因此透明地替换旧接口。

#5


1  

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.

Stat总是比fseek更好地确定文件大小,它将在非文件上安全地失败。64位filesizes是一个特定于操作的东西,您可以在命令的末尾加上“64”,或者您可以强制它在默认情况下执行所有的标准调用64。有关详细信息,请参阅编译器手册。

#6


0  

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

至少在linux上,您可以使用lseek64而不是fseek。

#1


5  

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

如果你在Windows中,你需要GetFileSizeEx (MSDN)。返回值是64位整数。

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

在linux stat64 (manpage)上是正确的。fstat如果您正在处理一个文件*。

#2


8  

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.

忽略所有出现“64”的答案。在Linux上,您应该向CFLAGS添加-D_FILE_OFFSET_BITS=64,并使用fseeko和ftello函数来获取/返回off_t值,而不是long。它们不是C的一部分,而是POSIX的一部分。其他(非linux) POSIX系统可能需要不同的选项来确保off_t是64位的;检查你的文档。

#3


2  

This code works for me in Linux:

这段代码适用于我在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;
}

#4


1  

(stolen from the glibc manual)

(从glibc手册偷来)

int fgetpos64 (FILE *stream, fpos64_t *position)

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.

此函数类似于fgetpos,但文件位置返回在fpos64_t类型的变量中,该变量指向哪个位置。

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.

如果源是在32位机器上使用_FILE_OFFSET_BITS == 64来编译的,这个函数可以在fgetpos的名称下使用,因此透明地替换旧接口。

#5


1  

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.

Stat总是比fseek更好地确定文件大小,它将在非文件上安全地失败。64位filesizes是一个特定于操作的东西,您可以在命令的末尾加上“64”,或者您可以强制它在默认情况下执行所有的标准调用64。有关详细信息,请参阅编译器手册。

#6


0  

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

至少在linux上,您可以使用lseek64而不是fseek。