我确定我刚刚在手册中错过了这篇文章,但是如何使用C++的istream
头文件从fstream
头中确定文件的大小(以字节为单位)?使用C++文件流(fstream),如何确定文件的大小?
回答
您可以打开使用ios::ate
标志(和ios::binary
标志)的文件,所以tellg()
功能会直接给你文件大小:
ifstream file("example.txt", ios::binary | ios::ate);
return file.tellg();
@Dominik Honnef:在VS 2013 Update5 64位这种方法,与_ios:ate_标志和没有_seekg(0,ios:结束)_可能不适用于大型文件。有关更多信息,请参阅http://stackoverflow.com/questions/32057750/how-to-get-the-filesize-for-large-files-in-c。 –
这似乎不是一个好方法。 [tellg不会报告文件的大小,也不会以字节开头的偏移量](http://stackoverflow.com/a/22986486/1835769)。 – displayName
@displayName [cplusplus.com](http://www.cplusplus.com/reference/istream/istream/seekg/)有一种不同意这种说法:它确实使用'tellg()'来检测文件大小。 –
您可以寻求到年底,然后计算的区别:
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;
}
像这样:
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;
您可能希望使用更合适的'std :: streampos'而不是'long',因为后者可能不支持像前者那么大的范围 - 并且'streampos' *不仅仅是一个整数。 –
我是一个新手,但是这是做的我自学的方式:
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.
时,它在大量情况下返回0,所有这些决定是否存在第一个字符。这有什么帮助? – warren
不要使用tellg
,以确定文件的确切大小。由tellg
确定的长度将大于可从文件中读取的字符数。
从stackoverflow问题tellg() function give wrong size of file?tellg
不报告文件的大小,也没有报告从字节开始的偏移量。它会报告一个令牌值,稍后可以用来找到相同的地方,仅此而已。 (甚至不能保证你可以将类型转换为整型。)。对于Windows(以及大多数非Unix系统),在文本模式下,tellg返回值与您必须读取以获取该位置的字节数之间没有直接和即时映射。
如果确切知道您可以读取多少字节非常重要,那么可靠地这样做的唯一方法就是阅读。您应该能够用的东西要做到这一点,如:
#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);
- 1. 码流中的fstream文件大小
- 2. 确定在上传流中使用imagepng的文件大小
- 3. Bash确定文件大小
- 4. Javascript - 确定文件大小
- 5. 确定文件夹大小
- 6. 文件流与<fstream>
- 7. 使用fstream从C++的* .txt文件读取数字使用fstream
- 8. 如何确定前的文件大小使用PHP卷曲
- 9. 如何使用Hive确定HDFS中的文件大小
- 10. 限制文件流的文件大小?
- 11. 如何使用fstream从文件读取特定行(C++)
- 12. 如何查找文件末尾以确定文件大小?
- 13. 如何使用C#按文件大小拆分PDF文件?
- 14. 使用C#更大的文件流#
- 15. 如何使用FileSystemObject.Size可靠地确定文件夹大小?
- 16. 使用fstream读取文件
- 17. PHP,确定远程文件的大小
- 18. 确定图像文件大小的PHP
- 19. C++ fstream多输入文件
- 20. 如何读取特定文件fstream?
- 21. 如何获取C文件的大小?
- 22. 如何确定C++中一个巨大的二进制文件的大小
- 23. 如何创建特定大小的文本文件C++
- 24. 如何使用标题确定精确的PE图像文件大小?
- 25. Linux:使用列格式化流(管道)输出? (未确定的文件大小)
- 26. 清除给定大小的文件C++
- 27. JavaScript来确定文件大小
- 28. 在VB.NET中确定文件大小
- 29. 确定使用的库,以减少JAR文件的大小
- 30. 使用文件对象之前确定文件对象的大小
http://stackoverflow.com/questions/2282549/c-getting-incorrect-file-size/2289423#2289423 –