二进制文件I / O. [英] Binary file I/O

查看:76
本文介绍了二进制文件I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的......我正在学习C ++。在我的旧(非便携式)
编程日里,我经常使用二进制文件......不用担心

关于endian问题。我开始理解为什么C ++会这样做

很难直接读取/写入一个整数作为文件的比特流。

但是,我是对于如何做以下事情有点不知所措。因此,不要混淆这个问题,我不会表现出我一直在尝试的东西;-)


我想做的是以下,使用标准IO流。


1)打开一个任意文件(file1)。

2)从(file1)中的第一个字节开始,将一大块数据读入

一个整数数组。

3)操纵数组,作为整数数据,然后输出内容
$ b $数组的b到另一个文件(file2)。

4)从file1读取下一个数据块到数组中。

5)转到3直到文件结束。


如果有人知道包含具体例子的教程,

我会感谢指向信息的指针。谢谢

OK...I''m in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I''m starting to understand why C++ makes it
difficult to read/write an integer directly as a bit-stream to a file.
However, I''m at a bit of a loss for how to do the following. So as
not to obfuscate the issue, I won''t show what I''ve been attempting ;-)

What I want to do is the following, using the standare IO streams.

1) open an arbitrary file (file1).
2) starting with the first byte in (file1), read a chunk of data into
an array of integers.
3) manipulate the array, as integer data, and then output the contents
of the array to another file (file2).
4) read the next data-chunk from file1 into the array.
5) goto 3 until end of file.

If anyone knows of a tutorial that contains concrete examples of this,
I''d appreciate a pointer to the info. Thanks

推荐答案

2003年7月24日16:56:53 -0700, ma ********** @ yahoo.com (J。Campbell)

写道:
On 24 Jul 2003 16:56:53 -0700, ma**********@yahoo.com (J. Campbell)
wrote:
好的......我正在学习C ++。在我的旧(非便携)编程时代,我大量使用二进制文件......不用担心关于字节序的问题。我开始明白为什么C ++使得难以直接读取/写入一个整数作为文件的比特流。
但是,我有点不知所措做以下事情。所以,为了不混淆这个问题,我不会展示我一直在尝试的东西;-)

我想做的是以下,使用标准IO流。


#include< fstream>

#include< iostream>

#include< vector>

#include< sstream>

#include< string>

#include< algorithm>


1)打开任意文件(file1)。


std :: ifstream file1(" f.txt");

2)从(file1)的第一个字节开始,读取一大块数据转换为整数数组。


const int CHUNK = 128;


char buffer [CHUNK];

file1.read(buffer, CHUNK);


std :: vector< int>数据;

std :: copy(缓冲区,缓冲区+ 128,std :: back_inserter(数据));

3)操纵数组,作为整数数据,


void操纵(std :: vector< int>& v);

操纵(数据);

然后输出数组的内容到另一个文件(file2)。


std :: ofstream file2(" g.txt");;

std :: copy(data.begin(),data.end( ),

std :: ostream_iterator< int>(std :: cout," \ n"));

4)从file1读取下一个数据块进入阵列。
5)转到3直到文件结束。


转到3; :)

如果有人知道包含具体例子的教程,
我会感谢指向信息的指针。谢谢
OK...I''m in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I''m starting to understand why C++ makes it
difficult to read/write an integer directly as a bit-stream to a file.
However, I''m at a bit of a loss for how to do the following. So as
not to obfuscate the issue, I won''t show what I''ve been attempting ;-)

What I want to do is the following, using the standare IO streams.
# include <fstream>
# include <iostream>
# include <vector>
# include <sstream>
# include <string>
# include <algorithm>

1) open an arbitrary file (file1).
std::ifstream file1("f.txt");
2) starting with the first byte in (file1), read a chunk of data into
an array of integers.
const int CHUNK = 128;

char buffer[CHUNK];
file1.read(buffer, CHUNK);

std::vector<int> data;
std::copy(buffer, buffer + 128, std::back_inserter(data));
3) manipulate the array, as integer data,
void manipulate(std::vector<int> &v);
manipulate(data);
and then output the contents
of the array to another file (file2).
std::ofstream file2("g.txt");;
std::copy(data.begin(), data.end(),
std::ostream_iterator<int>(std::cout, "\n"));
4) read the next data-chunk from file1 into the array.
5) goto 3 until end of file.
goto 3; :)
If anyone knows of a tutorial that contains concrete examples of this,
I''d appreciate a pointer to the info. Thanks




Josuttis的C ++标准库。


Jonathan



The C++ Standard Library by Josuttis.

Jonathan

< br>

2003年7月24日星期四20:39:23 -0400,Jonathan Mcdougall

< DE ***************** *@yahoo.ca>写道:
On Thu, 24 Jul 2003 20:39:23 -0400, Jonathan Mcdougall
<DE******************@yahoo.ca> wrote:
2003年7月24日16:56:53 -0700, ma**********@yahoo.com (J。坎贝尔)
写道:
On 24 Jul 2003 16:56:53 -0700, ma**********@yahoo.com (J. Campbell)
wrote:
好的......我是在学习C ++的过程中。在我的旧(非便携)编程时代,我大量使用二进制文件......不用担心关于字节序的问题。我开始明白为什么C ++使得难以直接读取/写入一个整数作为文件的比特流。
但是,我有点不知所措做以下事情。所以,为了不混淆这个问题,我不会展示我一直在尝试的东西;-)

我想做的是以下,使用标准IO流。
#include< fstream>
#include< vector>
#include< algorithm>
OK...I''m in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I''m starting to understand why C++ makes it
difficult to read/write an integer directly as a bit-stream to a file.
However, I''m at a bit of a loss for how to do the following. So as
not to obfuscate the issue, I won''t show what I''ve been attempting ;-)

What I want to do is the following, using the standare IO streams.
# include <fstream>
# include <vector>
# include <algorithm>




忘了这些:

#include< sstream>
#include< iostream>
#include< string>



Forget these ones :
# include <sstream>
# include <iostream>
# include <string>

1)打开一个任意文件(file1)。
std :: ifstream file1(" f.txt");
1) open an arbitrary file (file1).
std::ifstream file1("f.txt");
2)从第一个字节开始( file1),将一大块数据读入一个整数数组。
2) starting with the first byte in (file1), read a chunk of data into
an array of integers.



const int CHUNK = 128;

char buffer [CHUNK];
file1.read(buffer,CHUNK);

std :: vector< int>数据;
std :: copy(缓冲区,缓冲区+ 128,std :: back_inserter(数据));



const int CHUNK = 128;

char buffer[CHUNK];
file1.read(buffer, CHUNK);

std::vector<int> data;
std::copy(buffer, buffer + 128, std::back_inserter(data));




std :: copy(缓冲区,缓冲区) + CHUNK,std :: back_inserter(data));



std::copy(buffer, buffer + CHUNK, std::back_inserter(data));

3)操纵数组,作为整数数据,
3) manipulate the array, as integer data,




:: vector< int>& v);

操纵(数据);



void manipulate(std::vector<int> &v);
manipulate(data);

然后将数组的内容输出到另一个file(file2)。
and then output the contents
of the array to another file (file2).



std :: ofstream file2(" g.txt");;
std :: copy(data.begin(),data.end (),
std :: ostream_iterator< int>(std :: cout," \ n"));



std::ofstream file2("g.txt");;
std::copy(data.begin(), data.end(),
std::ostream_iterator<int>(std::cout, "\n"));




std :: copy( data.begin(),data.end(),

std :: ostream_iterator< int>(file2," \ n"));

对不起,


Jonathan



std::copy(data.begin(), data.end(),
std::ostream_iterator<int>(file2, "\n"));
Sorry about that,

Jonathan


谢谢Jonathan。


您的回复最有帮助的。现在,我需要消化它为什么会起作用,

以及为什么它是必要的。


我想要澄清一些事情。假设int是32位,那么,

之后:

-----

const int CHUNK = 128;


char buffer [CHUNK];

file1.read(buffer,CHUNK);

------

此时为char数组,buffer为包含128个1字节元素

,对吗?


-----

std :: vector< int> ;数据;

std :: copy(缓冲区,缓冲区+ 128,std :: back_inserter(数据));

-----

现在,名为data的向量包含32个元素,每个元素都是一个4字节的整数,对吧?


我如何知道进入向量整数的字节是否去了在

头先或先脚?换句话说,如果

文件的前4个字节是(HEX):

FF 00 00 00

将是第一个int in矢量数据是FF000000(dec 4278190080)

还是000000FF(dec 255)?或者它是否依赖于机器?


我可以避免所有的std ::通过使用using namespace std;或者是范围 - 解析所有关键字所需的



另一件事......你认为阅读大块的更好吗?文件为

我已经指出,或者将整个文件加载到内存中是否更好?


另外,你的方法留下2个重复的文件内存中的数据......一个是char数组,一个是
,另一个是向量。这是一个问题吗?


还有一件事......我最近在这里问了一个问题:

http:/ /groups.google.com/groups?hl=e...gle.com&rnum=1

关于以int数组的形式访问char数组。矢量

方法与早期帖子中演示的(不安全和非便携)方法不同/更安全。




再次感谢你的帮助。


我似乎无法退出打字;-)抱歉给你带来了好处

这么多问题......我意识到你可能不会选择解决这些问题

all ..
Thanks Jonathan.

Your response is most helpful. Now, I need to digest why it works,
and why it''s necessarry.

I want to clairify a few things. Assuming int is 32-bits, then,
after:
-----
const int CHUNK = 128;

char buffer[CHUNK];
file1.read(buffer, CHUNK);
------
at this point the char array, "buffer" contains 128 elements of 1-byte
each, right?

-----
std::vector<int> data;
std::copy(buffer, buffer + 128, std::back_inserter(data));
-----
now, the vector named "data" contains 32 elements, each of which is a
4-byte integer, right?

How do I know if the bytes that went into the vector integers went in
head-first or feet-first? in other words, if the first 4 bytes of the
file were (HEX):
FF 00 00 00
will the first int in the vector "data" be FF000000 (dec 4278190080)
or will it be 000000FF (dec 255)? Or is it machine dependent?

can I avoid all the "std::" by using "using namespace std;" or is it
necessary to scope-resolve all the keywords?

Another thing... Do you think it''s better to read chunks of a file as
I''ve indicated, or is it better to load the whole file into memory?

Also, your method leaves 2-duplicates of the data in memory...one as
the char array, and once as the vector. is this a problem?

One more thing...I asked a question here recently:

http://groups.google.com/groups?hl=e...gle.com&rnum=1

about accessing a char array as an array of int. How is the vector
method different/safer than the (unsafe & non-portable) method I
demonstrated in the earlier post.

thanks again for the help.

I don''t seem to be able to quit typing;-) Sorry to innundate you with
so many questions...I realize that you may not choose to address them
all..


这篇关于二进制文件I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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