#include <fstream>
#include <string>
#include <cassert>
long long GetFileSizeA(const std::string& file_path)
{
return std::ifstream
{
file_path, std::ios::ate
}.tellg();
}
long long GetFileSizeB(const std::string& file_path)
{
return std::ifstream
{
file_path, std::ios::ate | std::ios::binary
}.tellg();
}
int main()
{
auto a = GetFileSizeA("~/test.log");
auto b = GetFileSizeB("~/test.log");
assert(a == b); // always true?
}
如果文件~/test.log
包含許多\r\n
序列,並在C++標準保證GetFileSizeA
相同GetFileSizeB
?我必須在[ios :: binary]模式下打開一個文件來獲得它的大小嗎?