用C ++读取文件 [英] Reading files in C++

查看:79
本文介绍了用C ++读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个问题:



我正在使用C ++指针创建一个动态的二维数组(矩阵)。



然后将文件读入8 X n数组(矩阵)。



(size_of_file / 8)的剩余部分是不等于零。



出于同样的原因,我将有未填充的内存空间。



现在,如果我使用write命令写入文件的内容,即使垃圾值也会被写入新文件,它将被渲染为损坏。 />


使用 file_object>> 阅读无法正常工作,因为它错误。



读写命令被认为是用于写入和读取二进制文件的最佳。但它没有检测到\0字符,因此我可以限制复制\0和超出它的数据。







如何处理这个问题?

解决方案

文件中不需要像\0那样的东西。为什么?它当然没有被使用。 (而且,坦率地说,在内存中发明以0结尾的字符串表示的想法是灾难性的,但没有多少人理解它。)



-SA

Consider this problem :

I am using a C++ pointer to create a dynamic 2d array (matrix).

Then reading a file into 8 X n array (matrix).

The remainder of (size_of_file / 8) is not equal to zero.

For the same reason i will have unfilled memory spaces.

Now if i write the contents of the file using a write command , even the garbage values will be written into the new file and it will be rendered corrupt.

using file_object>> to read does not work properly as it erroneous.

Read and write commands are said to be the best for writing and reading of binary files. But it does not detect the \0 character so that i can restrict copying \0 and data beyond it.



How to handle this issue ?

解决方案

Nothing like \0 is ever needed in files. Why? And it''s of course not used. (And, frankly, the idea to invent 0-terminated strings presentation in memory was disastrous, but not many understand it.)

—SA


这篇关于用C ++读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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