使用 C++ 文件流 (fstream),如何确定文件的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:23:56  来源:igfitidea点击:

Using C++ filestreams (fstream), how can you determine the size of a file?

c++filesizefstreamistream

提问by warren

I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istreamclass from the fstreamheader?

我确定我刚刚在手册中遗漏了这一点,但是如何使用 C++ 的istream类从fstream标题中确定文件的大小(以字节为单位)?

回答by Pepus

You can open the file using the ios::ateflag (and ios::binaryflag), so the tellg()function will give you directly the file size:

您可以使用ios::ate标志(和ios::binary标志)打开文件,因此该tellg()函数将直接为您提供文件大小:

ifstream file( "example.txt", ios::binary | ios::ate);
return file.tellg();

回答by AraK

You can seek until the end, then compute the difference:

您可以搜索到最后,然后计算差异:

std::streampos fileSize( const char* filePath ){

    std::streampos fsize = 0;
    std::ifstream file( filePath, std::ios::binary );

    fsize = file.tellg();
    file.seekg( 0, std::ios::end );
    fsize = file.tellg() - fsize;
    file.close();

    return fsize;
}

回答by Jobin

Don't use tellgto determine the exact size of the file. The length determined by tellgwill be larger than the number of characters can be read from the file.

不要tellg用于确定文件的确切大小。由 确定的长度tellg将大于可以从文件中读取的字符数。

From stackoverflow question tellg() function give wrong size of file?tellgdoes not report the size of the file, nor the offset from the beginning in bytes. It reports a token value which can later be used to seek to the same place, and nothing more. (It's not even guaranteed that you can convert the type to an integral type.). For Windows (and most non-Unix systems), in text mode, there is no direct and immediate mapping between what tellg returns and the number of bytes you must read to get to that position.

从 stackoverflow 问题tellg() 函数给出错误的文件大小?tellg不报告文件的大小,也不报告从字节开始的偏移量。它报告一个令牌值,以后可以用来寻找同一个地方,仅此而已。(甚至不能保证您可以将类型转换为整数类型。)。对于 Windows(和大多数非 Unix 系统),在文本模式下,tellg 返回的内容与到达该位置必须读取的字节数之间没有直接和直接的映射。

If it is important to know exactly how many bytes you can read, the only way of reliably doing so is by reading. You should be able to do this with something like:

如果确切知道您可以读取多少字节很重要,那么可靠地这样做的唯一方法就是读取。您应该可以使用以下方法执行此操作:

#include <fstream>
#include <limits>

ifstream file;
file.open(name,std::ios::in|std::ios::binary);
file.ignore( std::numeric_limits<std::streamsize>::max() );
std::streamsize length = file.gcount();
file.clear();   //  Since ignore will have set eof.
file.seekg( 0, std::ios_base::beg );

回答by Philip

Like this:

像这样:

long begin, end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size: " << (end-begin) << " bytes." << endl;

回答by Chris X

I'm a novice, but this is my self taught way of doing it:

我是新手,但这是我自学的方法:

ifstream input_file("example.txt", ios::in | ios::binary)

streambuf* buf_ptr =  input_file.rdbuf(); //pointer to the stream buffer

input.get(); //extract one char from the stream, to activate the buffer
input.unget(); //put the character back to undo the get()

size_t file_size = buf_ptr->in_avail();
//a value of 0 will be returned if the stream was not activated, per line 3.