- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我做了一个示例项目来将文件读入缓冲区。当我使用 tellg() 函数时,它给我的值比read 函数实际上是从文件中读取的。我认为有一个错误。
这是我的代码:
编辑:
void read_file (const char* name, int *size , char*& buffer)
{
ifstream file;
file.open(name,ios::in|ios::binary);
*size = 0;
if (file.is_open())
{
// get length of file
file.seekg(0,std::ios_base::end);
int length = *size = file.tellg();
file.seekg(0,std::ios_base::beg);
// allocate buffer in size of file
buffer = new char[length];
// read
file.read(buffer,length);
cout << file.gcount() << endl;
}
file.close();
}
主要:
void main()
{
int size = 0;
char* buffer = NULL;
read_file("File.txt",&size,buffer);
for (int i = 0; i < size; i++)
cout << buffer[i];
cout << endl;
}
最佳答案
tellg
不报告文件的大小,也不报告偏移量从头开始,以字节为单位。它报告一个 token 值,它可以后来习惯找同一个地方,仅此而已。(甚至不能保证您可以将类型转换为整数类型。)
至少根据语言规范:在实践中,在 Unix 系统上,返回的值将是以字节为单位的偏移量从文件的开头开始,在 Windows 下,它将是对于在 中打开的文件,从文件开头的偏移量二进制模式。对于 Windows(和大多数非 Unix 系统),文本模式,什么之间没有直接和直接的映射 tellg
返回和必须读取的字节数那个位置。在 Windows 下,您真正可以依靠的是该值将不小于您拥有的字节数阅读(在大多数实际情况下,不会太大,虽然最多可以增加两倍)。
如果确切知道可以读取多少字节很重要,唯一可靠的方法是阅读。你应该能够通过以下方式做到这一点:
#include <limits>
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 );
最后,关于您的代码的另外两点:
第一行:
*buffer = new char[length];
不应该编译:你声明了 buffer
成为 char*
,所以*buffer
有类型 char
, 并且不是指针。鉴于什么你似乎在做,你可能想声明 buffer
作为一个 char**
.但更好的解决方案是声明它作为 std::vector<char>&
或 std::string&
. (这样,你也不必返回大小,并且不会泄漏内存如果有异常。)
第二,最后的循环条件错误。如果你真的想一次读一个字符,
while ( file.get( buffer[i] ) ) {
++ i;
}
应该可以解决问题。更好的解决方案可能是读取数据 block :
while ( file.read( buffer + i, N ) || file.gcount() != 0 ) {
i += file.gcount();
}
甚至:
file.read( buffer, size );
size = file.gcount();
编辑:我刚刚注意到第三个错误:如果您无法打开文件,你不告诉来电者。至少,你应该设置size
为 0(但某种更精确的错误处理可能更好)。
关于c++ - tellg() 函数给出错误的文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984956/
ifstream::tellg() 为某个文件返回 -13。 基本上,我编写了一个分析一些源代码的实用程序;我按字母顺序打开所有文件,我从“Apple.cpp”开始,它工作得很好..但是当它到达“Co
为什么tellg()在写操作后移动,我想应该是tellp()? std::fstream fs("c:\\log.txt", std::ios::in | std::ios::out | std::i
我有一个从文件中读取行的函数。但在读取之前,它会返回要读取下一行的地址。 我的职能是: void print() { int j=0; string a,b,c
我正在用 c++98 编写一个小解析器(是的,不能使用 11)。我正在使用 std::stringstream,我通过引用传递给不同的函数,让我们称它们为子解析器。为了知道要调用哪个子解析器,我需要知
我以二进制模式打开了一个文件,进行以下操作得到了 x 的负值。我打开的文件大小约为 2.5 GB。 infile.seekg(0, ios::end); __int64 x = infile.tell
我有一个正在读取的大型二进制文件,我想将当前位置与一个 unsigned long long int 进行比较。但是,根据 C++ 文档,我不清楚是否: tellg() 的返回类型是什么 如何比较 t
我做了一个示例项目来将文件读入缓冲区。当我使用 tellg() 函数时,它给我的值比read 函数实际上是从文件中读取的。我认为有一个错误。 这是我的代码: 编辑: void read_file (c
所以,我很好奇,因为我一直在使用这种相当有用的方法来获取文件的大小,但有些事情困扰着我。将流锁定到文件系统上的文件和 fileStream.seekg(0, std::ios::beg); int b
我下载了一个 C++ 实用程序,但在读取输入文件时由于错误而死掉了。不幸的是,它输出的只是“偏移量错误:文件中的 69570744324”;通过查看它的源代码,我发现它实际上是在那里打印出 .tell
我遇到了一个奇怪的问题。假设我正在读取这样的文件: std::ifstream in("file.txt", std::ios::binary); std::string text; in.seekg
我一直在 Visual Studio 2015 中使用 iostream 进行一些基本的文件操作,并注意到这种奇怪的行为。 #include #include #include void mai
假设我想获得两个 tellp() 输出之间的差异(整数)。 如果写入一个大文件,tellp() 的输出可能会很大,所以将它存储在一个 long long 中是不安全的。有没有安全的方法来执行这样的操作
我知道这个标题听起来很疯狂,但我现在正在亲 body 验这一点,我想不出任何失败的原因。 我正在使用 getline() 读取文件 在阅读结束时,我调用了 tellg()。但是,此调用总是失败(返回值
读取.cpp #include #include using namespace std; int main(void) { int id; char name[50];
我正在尝试使用 fstream 从文件中读取。我正在尝试的文件阅读有这样的内容: 1200 1000 980 890 760 我的代码: #include #include using names
我正在读取 data.txt: ////////////////////////////////////////////////// data.txt: ///////////////////////
我有一个 std::fstream,我使用它导入 std::fstream myFile { "C:/path/to/file.txt" }; 当我想读取第一个字节时,我使用 char c; cout
与 fstream file("test.txt",ios_base::in | ios_base::out | ios_base::binary); tellp 和 tellg 对于 file 是否
我在 std::fstream 类的 tellg() 函数中遇到问题。通常它应该返回当前字符在输入流中的位置。但是,它对我来说很奇怪。下面是一些简短的示例代码: #include #include
我正在编写一个 C++ 控制台应用程序。在用 size(int) rowSize 和 columnSize 创建了一个 Matrix 之后,我想将文本文件中的字母写入矩阵,但是 while 循环永远不
我是一名优秀的程序员,十分优秀!