process lotto。txt and get a correctly formatted data stream。
Piping Binary Data
When working with the console; for the most part; you will be transferring text data from one
process to another or from one file to another file。 However; when developing in ; working
…………………………………………………………Page 291……………………………………………………………
CH A PT E R 1 0 ■ L E A R N I N G A B O U T P E R S IS T E N CE 269
with text is not always the best choice。 A more efficient approach might be to store the data in
a binary format。
The easiest way to understand the difference between text data and binary data is to open
a binary file using a text editor; as shown in Figure 10…8。
Figure 10…8。 A binary file in a text editor
In Figure 10…8; you see just a bunch of funny characters with some text scattered throughout。
A binary file is different from a text file in that the format of a binary file is whatever the program
that reads and writes the binary data decides。 The advantage of using a binary file is that you
can create plex structures。 The disadvantage is that only the developer of the reader/writer
program knows what the file contains。
Binary files are not always smaller; but they are more efficient because you do not need to
parse and process the data。 For example; in the text…based lottery data stream reader; we needed
to parse every single line and then split the line of text into individual fields; which were then
parsed as integers and assigned to a variable。 Using a binary file; you only need to create a
binary type; and read or write that type。
In this section; we will continue with the example of the lottery…prediction application; but
this time; create it as a console application that converts a text data stream into a binary data
stream and then back to a text stream。 You’ll see how you can pipe the data from one stream to
another by sending it from one application to another application。 When we are finished
building this application; the following mand line will be valid。
type lotto。txt | TextProcessor。exe | Text2Binary。exe | Binary2Text。exe
The mand line starts with generating a text data stream using the type mand。
The mand TextProcessor。exe generates a clean data stream that is then piped into
…………………………………………………………Page 292……………………………………………………………
270 CH AP T E R 1 0 ■ L E A R N I N G A B OU T P E R S IS TE N CE
Text2Binary。exe; which generates a binary data stream。 Finally; the binary data stream is
converted back into a text stream using Binary2Text。exe; which displays the data on the console。
The architecture of TextProcessor with its implementation of the IProcessor interface
and a general architecture of grabbing a file or console input/output stream worked。 However;
IProcessor is not usable for binary streams; so we need to implement a new interface and
architecture。
■Note The examples that follow illustrate a very mon situation where an architecture worked and the
ideas of the architecture could be applied in the new context。 What does not work is the actual implementa
tion of the original architecture。 You may be tempted to modify the working architecture so that it will work
with the new context。 As much as you would like to do that; don’t。 Often; the abstractions you create will
plicate the architecture and make the resulting code more plex。 Your challenge is to know when to
generalize an architecture and when to keep the ideas of an architecture but create a new implementation;
and you’ll learn this with experience。
Defining the Interfaces and Implementing the Shell
In ; the data streams are split into two types of streams: binary and text。 The TextWriter
and TextReader types are used to read text…based data streams。 As was demonstrated with the
StringReader type; when dealing with text…based streams; certain assumptions can be made;
such as being able to find new lines in the stream。 With binary data streams; no such assump
tions can be made。 Binary streams have their own format; which is known by only the program
doing the reading or writing。
The binary stream…based types can be used to process text data streams; but doing so would
require knowing the details of the data stream。 Remember that gives you a text…handling
system that understands the different Unicode code pages。 A Unicode code page is a specific
translation map。 If you decide to manipulate the text streams using binary stream types; you
are telling that you will manage the details of the Unicode code pages。 Of course; you
don’t want to do that; and thus should never mix data streams。 So; for our sample application;
we need to design two different interfaces: one to stream from text to binary and one to stream
from binary to text。
■Note For more information about Unicode and other text…related issues; see the MSDN “International Text
Display” section (http://msdn2。microsoft。/en…us/library/ms776131。aspx)。
The following is the binary…to…text data stream interface; Binary2Text。IBinary2TextProcessor。
Imports System。IO
Public Interface IBinary2TextProcessor
Sub Process(ByVal input As Stream; ByVal output As TextWriter)
End Interface
…………………………………………………………Page 293……………………………………………………………
CH A PT E R 1 0 ■ L E A R N I N G A B O U T P E R S IS T E N CE 271
The IBinary2TextPr
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 最新章:第230章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章