包含文本和二进制数据的文件 [英] file containing text and binary data

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

问题描述

hi,我想创建一个同时包含文本数据和二进制数据的文件.这是创建此效果的最佳方法. MFC提供的CStdioFile可以实现相同的目的.但是在C#中,这是该类的等效类,或者在其他一些类中,它们提供相同的功能.

数据应该是这样的

文件输出:

hi I want to create a file which contains both text data and binary data. Which is the best possible way to create this. CStdioFile provided by MFC allows to achieve the same. But in C# which is the equivalent class for this or some other class which provides the same functionality.

the data should be like this

File output:

//text data- human readable
PersonName : Rama
Age : 22

//binary data
  @I@-@    Qi*@    >÷À    ØŽ!@   @S›%À   àíÔÀ   ÀÕ†@   à÷ß*À   à0’*@   à@   @wB@   €Ð-@    ¡ÙÀ    -øâ?   à¿	À    \ÑÀ   €ÔÞ@   ÀÉ"À    l@   @÷¤@   €¼RÀ    ¤@    ÆÈÀ    Ý¤@   €}?



删除虚假代码块-OriginalGriff [/edit]



[edit]Spurious code block removed - OriginalGriff[/edit]

推荐答案

我建​​议您使用类System.IO.BinaryWriterSystem.IO.BinaryReader(而不是流类) .他们可以读写二进制和文本数据.请注意,所有数据本质上都是二进制的.文本数据只是一种二进制.

对于写入或读取数据,请对每种数据类型(包括字符串)使用重载方法WriteRead.

现在,关于文字.请注意,作家和读者的构造函数允许指定编码.这很重要,因为可以用不同的编码来编写文本.写入和读取相同数据时,请确保编码匹配.

参见:
http://msdn.microsoft.com/en-us/library/system.io. binarywriter.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.io. binaryreader.aspx [ ^ ].

这是您的代码示例的外观:

I advise you to use the classes System.IO.BinaryWriter and System.IO.BinaryReader (and not stream classes). They can read and write both binary and text data. Note that all data is essentially binary. Text data just a sort of binary.

То write or read data, use overloaded methods Write or Read for each data type, including string.

Now, about the text. Pay attention that the writer and reader constructors allow to specify encoding. This is important, because the text can be written in different encoding. Make sure encodings match when you write and read the same data.

See:
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

This is how your code sample should look:

using (
    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(
        System.IO.File.Open(
            "my_binary_sample.rda",
            System.IO.FileMode.CreateNew),
               System.Text.Encoding.UTF8))
{
                  double[] binary = new double[] { 123, 456, 8, 9, 0, 7, 6, 5, 5, 4, 3, 3 };
                  writer.Write("Line 1");
                  writer.Write("Line 2");
                  writer.Write("Line 3");
                  foreach (double val in binary)
                     writer.Write(val);
}



检出"using".这很重要,因为即使发生异常,它也可以确保关闭流.

—SA



Check out "using". It is important as it guarantees closing of streams even if exception occurs.

—SA


那有多难?我用谷歌搜索了"C#编写二进制文件",下面的两个是第一个:

How hard can that be? I googled "C# write binary file" and these two below are the very first:


  1. http://www.vcskicks.com/write-binary-file.php [ ^ ]
  2. http://www.c -sharpcorner.com/Resources/733/how-to-read-and-write-binary-data-files-in-C-Sharp.aspx [

  1. http://www.vcskicks.com/write-binary-file.php[^]
  2. http://www.c-sharpcorner.com/Resources/733/how-to-read-and-write-binary-data-files-in-C-Sharp.aspx[^]



现在,如果您想知道我是如何想到这些不太可能的搜索字词的,那么我必须承认我没有任何线索. ;)

干杯!

—MRB



Now if you are wondering how I came to think of these unlikely search terms, I must confess I don''t have a clue. ;)

Cheers!

—MRB


这篇关于包含文本和二进制数据的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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