and*_*wrk 132 c io file filesize
如何计算文件的大小(以字节为单位)?
#include <stdio.h>
unsigned int fsize(char* file){
//what goes here?
}
Run Code Online (Sandbox Code Playgroud)
Ted*_*val 137
基于NilObject的代码:
#include <sys/stat.h>
#include <sys/types.h>
off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
Run Code Online (Sandbox Code Playgroud)
变化:
const char
.struct stat
缺少变量名称的定义.-1
错误而不是0
,这对于空文件来说是不明确的.off_t
是签名类型,所以这是可能的.如果要fsize()
在出错时打印消息,可以使用以下命令:
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
fprintf(stderr, "Cannot determine size of %s: %s\n",
filename, strerror(errno));
return -1;
}
Run Code Online (Sandbox Code Playgroud)
在32位系统上,您应该使用该选项编译它-D_FILE_OFFSET_BITS=64
,否则off_t
只能保存最大2 GB的值.有关详细信息,请参阅Linux中的大文件支持的"使用LFS"部分.
Ori*_*rds 73
不要用int
.如今,超过2千兆字节的文件很常见
不要用unsigned int
.超过4千兆字节的文件通常是一些略微不常见的污垢
IIRC标准库定义off_t
为无符号64位整数,这是每个人都应该使用的.当我们开始有16个exabyte文件时,我们可以在几年内将其重新定义为128位.
如果你在Windows上,你应该使用GetFileSizeEx - 它实际上使用一个带符号的64位整数,因此它们将开始遇到8个exabyte文件的问题.愚蠢的微软!:-)
Der*_*ark 30
Matt的解决方案应该有效,除了它是C++而不是C,并且初始告诉不应该是必要的.
unsigned long fsize(char* file)
{
FILE * f = fopen(file, "r");
fseek(f, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(f);
fclose(f);
return len;
}
Run Code Online (Sandbox Code Playgroud)
为你修好了你的支具.;)
更新:这不是最好的解决方案.这是仅限于在Windows 4GB的文件,它很可能比只使用一个特定于平台的调用,比如慢GetFileSizeEx
或stat64
.
and*_*wrk 14
**不要这样做(为什么?):
引用我在网上找到的C99标准文档:"将文件位置指示器设置为文件结尾,与fseek(文件,0,SEEK_END)一样,具有二进制流的未定义行为(因为可能是尾随空字符)或对于任何具有状态相关编码的流,它不能确定地在初始移位状态结束.**
将定义更改为int以便可以传输错误消息,然后使用fseek()和ftell()来确定文件大小.
int fsize(char* file) {
int size;
FILE* fh;
fh = fopen(file, "rb"); //binary mode
if(fh != NULL){
if( fseek(fh, 0, SEEK_END) ){
fclose(fh);
return -1;
}
size = ftell(fh);
fclose(fh);
return size;
}
return -1; //error
}
Run Code Online (Sandbox Code Playgroud)
小智 7
该POSIX标准都有自己的方法来获取文件的大小。
包括sys/stat.h
标题以使用该功能。
stat(3)
。st_size
属性。注意:大小限制为4GB
。如果不是Fat32
文件系统,则使用64位版本!
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
struct stat info;
stat(argv[1], &info);
// 'st' is an acronym of 'stat'
printf("%s: size=%ld\n", argv[1], info.st_size);
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
struct stat info;
stat(argv[1], &info);
// 'st' is an acronym of 'stat'
printf("%s: size=%ld\n", argv[1], info.st_size);
}
Run Code Online (Sandbox Code Playgroud)
的ANSI C不直接提供,以确定文件的长度的方式。
我们必须要动脑子。现在,我们将使用搜索方法!
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
struct stat64 info;
stat64(argv[1], &info);
// 'st' is an acronym of 'stat'
printf("%s: size=%ld\n", argv[1], info.st_size);
}
Run Code Online (Sandbox Code Playgroud)
文件是
stdin
还是管道。POSIX,ANSI C无法正常工作。 如果文件是管道或
,它将返回。0
stdin
意见:您应该改用POSIX标准。因为,它具有64位支持。
如果您使用std c库很好:
#include <sys/stat.h>
off_t fsize(char *file) {
struct stat filestat;
if (stat(file, &filestat) == 0) {
return filestat.st_size;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)