Vishwas
Vishwas

Reputation: 1541

Why byte level functions are required to read a text file?

Why byte level functions are required to read a text file. I mean after all it's a file containing an array of string. Then why it can't be stored in a string directly. Why in any language ( java, c, c++ or as3) byte level functions need to be used to read them ?

It could be quite easier if i could do something like this :

var a_str:String = new String();

var myFile:File  ;

a_str = String( myFile.read("xyz.txt") ) ;

trace ( a_str ) ; // << content of the file xyz.txt 

Upvotes: 0

Views: 183

Answers (4)

Marcin
Marcin

Reputation: 49826

There are plenty of languages which can read a whole file into a string. Python can; I'm pretty sure Perl can. That functionality is built on lower-level functionality that reads files a byte-at-a-time, of course (or, rather, as a sequence of bytes, whether or not a larger chunk is served up).

If you don't like the tools you're using, get some better ones.

Upvotes: 0

zje
zje

Reputation: 3912

The short answer? Memory is typically byte-addressable, so reading a file you would expect the same thing. In most C-style programming languages, a string is typically just a collection of bytes, typically terminated by null character, NUL (0x00).

Upvotes: 0

Guy Coder
Guy Coder

Reputation: 24976

How do you store an end of file with characters? For any reasonable length sequence of characters you can think of, there is a possibility that it will appear in the text and be treated as an end of file and prematurely ending the file.

Upvotes: 0

David W
David W

Reputation: 10184

Because not all text is rendered equally. Some older character sets represent themselves in one-byte characters, while other sets are multi-byte. As a result, writers for each have to be able to manipulate bytes, not just characters.

Upvotes: 1

Related Questions