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?

thanks,
rodchar

Re: excerpt from lecture notes help by Ben

Ben
Thu Jul 19 17:23:13 CDT 2007


"rodchar" <rodchar@discussions.microsoft.com> wrote in message
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?

Yes and no. Yes, you can write plain text randomly into a file, because
text is binary data. No, the automatic newline conversions for text files
(\n => CR+LF on Windows, no conversion on Unix) and random seeking don't
play well together.

So, as long as you do the newline conversion yourself and account for it
when seeking, all is ok. Just open the file in binary mode and write your
text strings.



Re: excerpt from lecture notes help by Giovanni

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



RE: excerpt from lecture notes help by rodchar

rodchar
Fri Jul 20 08:20:02 CDT 2007

thank you all for the feedback, this helps.

"rodchar" wrote:

> 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?
>
> thanks,
> rodchar

Re: excerpt from lecture notes help by rodchar

rodchar
Fri Jul 20 09:34:33 CDT 2007

please forgive me, the cpp newbie, but someone might have already gave me the
answer and i'm still missing it.

let me ask another way, if i may: is there a way to read and write
fixed-length text to a file and do all the random access stuff to it also?

"Ben Voigt [C++ MVP]" wrote:

>
> "rodchar" <rodchar@discussions.microsoft.com> wrote in message
> 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?
>
> Yes and no. Yes, you can write plain text randomly into a file, because
> text is binary data. No, the automatic newline conversions for text files
> (\n => CR+LF on Windows, no conversion on Unix) and random seeking don't
> play well together.
>
> So, as long as you do the newline conversion yourself and account for it
> when seeking, all is ok. Just open the file in binary mode and write your
> text strings.
>
>
>