我如何可以在C获取文件的大小? [英] How can I get a file's size in C?
本文介绍了我如何可以在C获取文件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
你如何确定一个文件的大小C 3
我怎样才能找到一个文件的大小?我打开用C语言编写的应用程序
我想知道大小,因为我希望把加载的文件的内容转换为字符串,这是我使用的Alloc 的malloc()
。只是写的malloc(10000 * sizeof的(炭));
恕我直言,是一个坏主意。
How can I find out the size of a file? I opened with an application written in C.
I would like to know the size, because I want to put the content of the loaded file into a string, which I alloc using malloc()
. Just writing malloc(10000*sizeof(char));
is IMHO a bad idea.
推荐答案
您需要寻求到文件的末尾,然后索要的位置:
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, e.g.:
fseek(fp, 0L, SEEK_SET);
或者(如果寻求去开头)
or (if seeking to go to the beginning)
rewind(fp);
这篇关于我如何可以在C获取文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文