Giovanni
Thu Jul 19 17:25:13 CDT 2007
"rodchar" <rodchar@discussions.microsoft.com> ha scritto nel messaggio
news:716F6BBB-2C74-489E-A8CC-8570EFBCE9B1@microsoft.com...
> hey all,
>
http://www.difranco.net/cop2334/Outlines/fileproc.htm
>
> there's a section called:
> Writing Data Randomly to a Random-access File
>
> can you make it write plain text to the text file and still make all the
> random stuff work? what do you need to changed to make it write as plain
> text
> instead of binary?
I did not read the whole article.
However, if you want to write text strings to a file, you just write the
characters of each string to the file. So, if you want to write the string
"Hello", you write the single characters: H, e, l, l, o to the file. Then
you should have a way to identify where one string ends and another string
starts.
A possible solution could be to terminate every string with a '\0' byte
character. So, to read a string, you read each bytes (as it would be binary
data) until you find the string terminating character '\0'.
Another option would be to store at the beginning of each string the number
of characters that this string is made by. So, you can skip to next string,
jumping the offset given by the number of string characters, e.g. you could
write the string "Hello" as:
5 H e l l o \0 <--- terminate with \0
^
|
+---- the 5 is the string length, in characters
If you have Unicode strings, you could extend the above schema using
encoding like UTF-16 or UTF-8.
Another way could be to assume a maximum length for each string (e.g. 50
characters) and write each string as a sequence of fixed count of characters
(the unused characters could be \0's), but this could waste lots of space...
It all depends on your needs...
Giov