简体   繁体   English

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

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

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 istream class from the fstream header?我确定我刚刚在手册中错过了这一点,但是如何使用来自fstream header 的 C++ 的istream class 确定文件的大小(以字节为单位)?

You can open the file using the ios::ate flag (and ios::binary flag), 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();

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;
}

Don't use tellg to determine the exact size of the file. 不要使用tellg来确定文件的确切大小。 The length determined by tellg will be larger than the number of characters can be read from the file. tellg确定的长度将大于可从文件中读取的字符数。

From stackoverflow question tellg() function give wrong size of file? 从stackoverflow问题tellg()函数给出错误的文件大小? tellg does not report the size of the file, nor the offset from the beginning in bytes. tellg不报告文件的大小,也不报告从字节开头的偏移量。 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. 对于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 );

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;

Since C++17, we have std::filesystem::file_size .从 C++17 开始,我们有了std::filesystem::file_size This doesn't strictly speaking use istream or fstream but is by far the most concise and correct way to read a file's size in standard C++.严格来说,这并不使用istreamfstream ,但它是迄今为止在标准 C++ 中读取文件大小的最简洁和正确的方法。

#include <filesystem>
...
auto size = std::filesystem::file_size("example.txt");

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM