二进制读取模式代码不起作用 [英] binary read mode code not working

查看:71
本文介绍了二进制读取模式代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main () {
	ifstream file ("C:\\Users\\filetypes.txt", ios::in|ios::binary|ios::ate);
	if (file.is_open())
	{
		size = file.tellg();
		memblock = new char [size];
		file.read (memblock, size);

		cout << "the complete file content is in memory";

		for (int l=0; l<size;>		{
			cout << " memblock[] is = " 
                             << (unsigned char)memblock[l] 
                             << " index was l = " << l << endl;

                }
        }		

}



使用上面的代码不是没有得到data.can任何一个请检查它。


using above code not am not getting the data.can any one check it please.

推荐答案

你用ios打开文件:ate使文件指针位于文件的末尾这就是为什么tellg会返回你需要的大小。然后你需要在调用read之前使用seekg将指针重置为文件的开头。

此致,

Ian。
You''re opening the file with ios:ate so that the file pointer is at the end of the file, which is why tellg returns you the size you need. You then need to reset the pointer to the start of the file, using seekg, before you call read.
Regards,
Ian.


这篇关于二进制读取模式代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆