简体   繁体   English

将aa文本文件转换为二进制stream?

[英]transform a a text file into a binary stream?

Is this code right?这段代码对吗?

If this is right, what would be the code for the opposite way like from Binary stream to text?如果这是正确的,那么从二进制 stream 到文本的相反方式的代码是什么?

#include <iostream>
#include <fstream>

int main()
{
   std::ifstream in("in.txt");
   std::ofstream out("out.bin", std::ios::binary);

   double d;
   while(in >> d) {
      out.write((char*)&d, sizeof d);
   }
}

THIS is the input file这是输入文件

A number of issues make the design of the video server, in a video-on-demand application, difficult.许多问题使视频点播应用中的视频服务器的设计变得困难。 First, a video server needs to simultaneously provide video services to multiple clients and guarantee the quality of service for each client.首先,视频服务器需要同时向多个客户端提供视频服务,并保证每个客户端的服务质量。 Second, a video server needs to manage system resources, including CPU/disk/memory, and schedule network activity so as to utilize maximally the resources, while not overloading the system.其次,视频服务器需要管理系统资源,包括 CPU/磁盘/内存,并调度网络活动,以最大限度地利用资源,同时不使系统过载。 Third, a video server needs to be able to support a variety of VCR operations such as playback, fast-forward, slow-forward, pause, resume, indexing, and scrolling.第三,视频服务器需要能够支持各种 VCR 操作,例如播放、快进、慢进、暂停、恢复、索引和滚动。 Finally, a user watching a video may change from one service to another service—for example, from playback to fast-forward or from playback to slow-forward.最后,观看视频的用户可能会从一种服务更改为另一种服务——例如,从回放到快进或从回放到慢进。 A video server should support these dynamic service changes while efficiently using system resources.视频服务器应支持这些动态服务更改,同时有效地使用系统资源。

Yes that is correct.对,那是正确的。 For the opposite, exchange write for read and your are halfway there.相反, writeread ,你就成功了。

Just note that the encoding of double and it's size might differ on different platforms/systems/compilers, therefore using this type of storage for anything then a temporary stash is not recommended.请注意, double精度的编码及其大小在不同的平台/系统/编译器上可能会有所不同,因此不建议将这种类型的存储用于任何东西,然后临时存储。

Is this code right?这段代码对吗?

That depends upon what you want it to do.这取决于你想要它做什么。 What your program does is to read in a text file consisting entirely of the text representation of rational numbers and white space (eg 1.2 5 99.0 ), and outputs a file consisting entirely of a binary representation of those same numbers.您的程序所做的是读取一个完全由有理数和空格的文本表示形式组成的文本文件(例如1.2 5 99.0 ),并输出一个完全由这些相同数字的二进制表示形式组成的文件。

Note that the details of how those numbers are represented is CPU- OS- and/or compiler-specific.请注意,这些数字如何表示的细节是特定于 CPU 操作系统和/或编译器的。

Is that what you wanted it to do?那是你想要它做的吗?

what would be the code for the opposite way相反方式的代码是什么

The opposite way would be to read in a file consisting of the binary representation of floating-point numbers and output a text file with the text representation of those same numbers.相反的方法是读入一个由浮点数的二进制表示组成的文件和 output 一个具有这些相同数字的文本表示的文本文件。 To do that, you would ifstream::read the doubles from an ios::bin file, and >> the numbers to the text file.为此,您将ifstream::readios::bin文件中读取双精度数,然后>>将数字写入文本文件。

Note that the conversion won't be exactly.请注意,转换不会完全正确。 That is, you will almost certainly not get precisely the same output file as you originally started with, but rather one that is semantically equivalent.也就是说,您几乎肯定不会得到与最初开始时完全相同的 output 文件,而是在语义上等价的文件。

Well, it depends on what you mean by "binary stream".好吧,这取决于您所说的“二进制流”是什么意思。 The diference between a file opened in text mode and binary mode is that in binary mode if you read/write a "\r", "\n" or "\r\n" they are read/written literally, whereas in text mode the characters that make up the line end markers on your system are transformed into "\n", see this以文本模式打开的文件和二进制模式之间的区别在于,在二进制模式下,如果您读/写“\r”、“\n”或“\r\n”,它们是按字面意思读/写的,而在文本模式下构成系统上的行结束标记的字符将转换为“\n”,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM