Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

c - 如何在C中获取文件大小? [重复](How can I get a file's size in C? [duplicate])

Possible Duplicate:

(可能重复:)
How do you determine the size of a file in C?

(如何确定C中文件的大小?)

How can I find out the size of a file I opened with an application written in C ?

(如何找出用C语言编写的应用程序打开的文件大小?)

I would like to know the size, because I want to put the content of the loaded file into a string, which I allocate using malloc() .

(我想知道大小,因为我想将加载文件的内容放入一个字符串,我使用malloc()分配。)

Just writing malloc(10000*sizeof(char));

(只写malloc(10000*sizeof(char));)

is IMHO a bad idea.

(恕我直言是一个坏主意。)

  ask by Nino translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to seek to the end of the file and then ask for the position:

(您需要寻找文件的末尾然后询问位置:)

fseek(fp, 0L, SEEK_END);
sz = ftell(fp);

You can then seek back, eg:

(然后你可以回头,例如:)

fseek(fp, 0L, SEEK_SET);

or (if seeking to go to the beginning)

(或者(如果想要开始))

rewind(fp);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...