如何将文本文件转换为二进制,反之亦然? [英] How to convert a text file into binary and vice versa?

查看:159
本文介绍了如何将文本文件转换为二进制,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使前面的问题更简单得到答案,并完全理解它。



问题是,我想编写一个C ++程序,将普通文本文件转换为二进制文件,然后读取该二进制文件并将其转换为文本文件,文本文件等于第一个文本文件。
我写了这个简单的代码。

  int main()
{

string name1 =first sec,name3 =third;
int j = 0,k = 0;

ifstream ifs(name1.c_str()); //这里我想从普通文本文件(name1)中读取。
ifs>> j; //现在j等于5,因为name1包含数字5.
ifs.close();

ofstream ofs(name2.c_str(),ios :: binary);
ofs.write(as_bytes(j),sizeof(int)); //这里我想以二进制模式将j写入name2文件。
ofs.close();

ifstream ifs1(name2.c_str(),ios :: binary); //这里我想从那个二进制文件(name2)中读取。
ifs.read(as_bytes(k),sizeof(int)); //这里我希望k变成5.

ofstream ofs1(name3.c_str());
ofs1<< k; //这里我想在普通文本模式下将k写入name3文件。

ifs1.close();
ofs1.close();
//现在我希望name1和name2都包含5。

keep_window_open();
return 0;
}

现在, ofs.write(as_bytes ),sizeof(int)); ifs.read(as_bytes(k),sizeof(int)); >

我的问题是为什么,实际上,文件 name1 包含数字5, 1字节。 name2 包含 [] 的一些字符/符号,其大小为4字节, name3 包含数字0,其大小为1字节?



我会完全感谢您的回复。



请不要通过提供额外的信息来扩大问题,而只是想想这个问题。我想只是理解它。 (我的机器是Windows 7 32位,我的编译器是MVS 2012.如果需要更多的信息,请告诉我。)

解决方案

写入和读取



写入方法用于二进制文件的i / o操作。



它们有以下原型:

  write(memory_block,size); 
read(memory_block,size);

c>



> read 从关联文件读取 size 个字节,并将其写入 memory_block



例如,在您的情况下,

  ofs.write(as_bytes ),sizeof(int)); 

j 的字节写入一个bynary文件名为 name2



您可以查看更多关于输入和输出的文件此处



为什么你没有在name2中输入五五



您将 j 的字节写入 name2 文件在二进制i / o模式。任何i / o操作都独立于此模式下的任何格式注意事项执行。写入数据后不添加回车。这意味着你不能读取 name2 文件,并看到那里的五个数字。



为什么你有name3文件中的零数字



原因是因为打字错误)



先看看行



  ifstream ifs(name1.c_str()); //这里我想从普通文本文件(name1)中读取。 
ifs>> j; //现在j等于5,因为name1包含数字5.
ifs.close();

然后看看这些行

  ifstream ifs1(name2.c_str(),ios :: binary); //这里我想从那个二进制文件(name2)中读取。 
ifs.read(as_bytes(k),sizeof(int)); //这里我希望k变成5.

您尝试从已关闭的文件流对象读取字节。对这些对象的任何操作都会出错。你可以这样检查:

  assert(!ifs.read(as_bytes(k),sizeof(int)) ); 

这可能是因为读取引用 ifs ifs 可以转换为布尔值。



由于本节中的所有内容,变量 k 的值保持不变。
您不能从关闭的文件读取,您不能更改 k 值。因为这样, k 的旧
值写入 name3 文件。



工作示例



  #include< assert.h> 
#include< fstream>
#include< string>

using namespace std;

typedef char byte;

template< typename T>
byte * as_bytes(T * ptr){
return reinterpret_cast< byte *>(ptr);
}

int main()
{

string
name1 =first.txt,
name2 = second.bin,
name3 =third.txt;

int j = 0,k = 0;

//这里我想从普通文本文件(name1)中读取。
ifstream ifs(name1.c_str());
ifs>> j;
//现在j等于5,因为name1包含数字5。
assert(j == 5);
ifs.close();

ofstream ofs(name2.c_str(),ios :: binary);
//这里我想以二进制模式将j写入name2文件。
ofs.write(as_bytes(& j),sizeof(int));
ofs.close();

//这里我想从那个二进制文件(name2)中读取。
ifstream ifs1(name2.c_str(),ios :: binary);
//这里我希望k变成5.
ifs1.read(as_bytes(& k),sizeof(int));


ofstream ofs1(name3.c_str());
//这里我想在普通文本模式下写k到name3文件。
ofs1<< k;

ifs1.close();
ofs1.close();
}


I have made the previous problem simpler to get the answer and understand it completely.

The problem is that I want to write a C++ program that converts an ordinary text file into binary and then reads that binary file and converts it to a text file so that this text file equals to first text file. I have wrote this simple code for it.

int main()
{

      string name1 = "first", name2 = "sec", name3 = "third";
      int j = 0, k = 0;

      ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
      ifs >> j; // Now j equals to 5 because name1 contains digit 5.
      ifs.close();

      ofstream ofs(name2.c_str(), ios::binary);
      ofs.write(as_bytes(j), sizeof(int)); // Here I want to write that j to name2 file in binary mode.                      
      ofs.close();

      ifstream ifs1(name2.c_str(), ios::binary); // Here I want to read from that binary file (name2).
      ifs.read(as_bytes(k), sizeof(int)); // Here I hope k becomes 5.

      ofstream ofs1(name3.c_str());
      ofs1 << k; // Here I want to write that k to name3 file in ordinary text mode.

      ifs1.close();
      ofs1.close();
      // Now I hope both name1 and name2 contain 5.

      keep_window_open();
      return 0;
}

Now what the ofs.write(as_bytes(j), sizeof(int)); or ifs.read(as_bytes(k), sizeof(int)); exactly means?

My question is why, in practice, the file name1 contains digit 5 and its size is 1 byte. The name2 contains some character/sign like [] and its size is 4 bytes and name3 contains digit 0 and its size is 1 byte?

I'll completely appreciate your responses.

Please don't extent the issue by giving extra info and only think around this problem. I want to just understand it. (My machine is Windows 7 32-bit. My compiler is MVS 2012. And if any more info is needed just tell me.)

解决方案

write and read

write and read methods are used for i/o operations with binary files.

They has the following prototypes:

write(memory_block, size);
read(memory_block, size);

The write writes size bytes from memory_block to associated file.

The read reads size bytes from associated file and writes it to a memory_block

For example, in your case,

ofs.write(as_bytes(j), sizeof(int));

writes bytes of j number into a bynary file named name2.

You can see more about input and output with files here.

Why you havn't number five in name2 five

You write the bytes of j variable to name2 file in binary i/o mode. Any i/o operation is performed independently of any format considerations in this mode. It don't add carriage return after written data. This means that you can't read the name2 file and see the five number there.

Why you have a zero number in name3 file

The reason is because of typo :)

First look at the lines

   ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
   ifs >> j; // Now j equals to 5 because name1 contains digit 5.
   ifs.close();

And then look at the lines

 ifstream ifs1(name2.c_str(), ios::binary); // Here I want to read from that binary file (name2).
 ifs.read(as_bytes(k), sizeof(int)); // Here I hope k becomes 5.

You are trying to read bytes from already closed file stream object. Any operation with such objects ends up with error. You can check it out in this way:

assert(!ifs.read(as_bytes(k), sizeof(int)));

It is possible because read returns a mutable reference to ifs and ifs is convertible to boolean value.

Because of all stuff above in this section, the value of variable k stay unchanged. You can't read from closed file, you can't change the k value. Because of this the old value of k is written to the name3 file.

Working example

#include <assert.h>
#include <fstream>
#include <string>

using namespace std;

typedef char byte;

template<typename T>
byte* as_bytes(T* ptr) {
    return reinterpret_cast<byte*>(ptr);
}

int main()
{

    string
        name1 = "first.txt",
        name2 = "second.bin",
        name3 = "third.txt";

    int j = 0, k = 0;

    // Here I want to read from the ordinary text file (name1).
    ifstream ifs(name1.c_str());
    ifs >> j;
    // Now j equals to 5 because name1 contains digit 5.
    assert(j == 5);
    ifs.close();

    ofstream ofs(name2.c_str(), ios::binary);
    // Here I want to write that j to name2 file in binary mode.
    ofs.write(as_bytes(&j), sizeof(int));                      
    ofs.close();

    // Here I want to read from that binary file (name2).
    ifstream ifs1(name2.c_str(), ios::binary);
    // Here I hope k becomes 5.
    ifs1.read(as_bytes(&k), sizeof(int));


    ofstream ofs1(name3.c_str());
    // Here I want to write that k to name3 file in ordinary text mode.
    ofs1 << k;

    ifs1.close();
    ofs1.close();
}

这篇关于如何将文本文件转换为二进制,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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