Qbasicnews.com
OPEN file routine adding extra "0" to output file. - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: OPEN file routine adding extra "0" to output file. (/thread-3432.html)

Pages: 1 2


OPEN file routine adding extra "0" to output file. - Dex4now - 03-09-2004

When I use this code . . .

Code:
CLS
file1$ = "C:\filetest.001"
file2$ = "C:\filetest.002"
a$ = CHR$(32)
count = 0
OPEN file1$ FOR BINARY AS #1
OPEN file2$ FOR BINARY AS #2
  DO UNTIL EOF(1)
    count = count + 1
    GET #1, count, a$
    PUT #2, count, a$
  LOOP
CLOSE
SYSTEM

. . . an extra zero gets added to the output file. (filetest.002)
So, filetest.002 is one byte larger than filetest.001.
I've tried every combination of:

DO WHILE NOT EOF(1)
DO UNTIL EOF(10
. . .
LOOP UNTIL EOF(1)
LOOP WHILE NOT EOF(1)

Not at the same time of course . . . :wink:

Can anyone help me figure this one out?
Also, this is a snippet from a larger project. Its important that I be able to transfer the bytes from one file to the other byte-by-byte.
Thats why I'm not "SHELL'ing" to the copy command, or something like that.

Thanks all, Dex


OPEN file routine adding extra "0" to output file. - Zack - 03-09-2004

Maybe, maybe this is the problem...
The eof() function might only (like with C...so annoying) return true if you have already read past the end of the file.
Try this:
Code:
CLS
file1$ = "C:\filetest.001"
file2$ = "C:\filetest.002"
a$ = CHR$(32)
count = 0
OPEN file1$ FOR BINARY AS #1
OPEN file2$ FOR BINARY AS #2
  DO
    count = count + 1
    GET #1, count, a$
    IF EOF(1) THEN EXIT DO
    PUT #2, count, a$
  LOOP
CLOSE
SYSTEM



OPEN file routine adding extra "0" to output file. - Dex4now - 03-09-2004

Thanks Zack. That does seem to work. Just seems strange that you'ld have to exit a DO loop in a somewhat unconventional manner.

I thought maybe this was that "little-endian" thing again. :normal:

Dex


OPEN file routine adding extra "0" to output file. - Zack - 03-09-2004

Hahah, wait til' you code in Assembly, little endian ends up strangling you in the end.
Works? Glad to hear it.


OPEN file routine adding extra "0" to output file. - Dex4now - 03-09-2004

Assembly . . . aaack! :o

I didn't notice til you suggested it, this line straight from the
QuickBASIC help file on EOF:

"When EOF is used with random-access or binary files, it returns "true" if the last executed GET statement was unable to read an entire record. This happens because of an attempt to read beyond the end of the file."

The trouble is, you need to know which command you're doing wrong before you know which HELP file to look under. :wink:

Dex


OPEN file routine adding extra "0" to output file. - Zack - 03-09-2004

Bingo.
I think it's the same in *every* MSDOS-based language, because the interrupt service for EOF-checking only returns true if you've read past the end of file marker. And since QB (and C, and all others) uses that interrupt service...


OPEN file routine adding extra "0" to output file. - Dex4now - 03-09-2004

Dang . . . as I was looking this thing over, I realized that I made it a whole bunch harder than it needed to be.

This works much better:

Code:
CLS
file1$ = "C:\TEMP\filetest.001"
file2$ = "C:\TEMP\filetest.002"
a$ = CHR$(32)
OPEN file1$ FOR BINARY AS #1
OPEN file2$ FOR BINARY AS #2
  count = LOF(1)
  FOR x = 1 TO count
    GET #1, x, a$
    PUT #2, x, a$
  NEXT x
CLOSE
SYSTEM

I keep forgetting about the LOF function.
Talking about it definitely helped me to find this. :wink:


OPEN file routine adding extra "0" to output file. - Zack - 03-09-2004

What's so hard about the IF...EXIT DO statement?
Mmm-hmm. Sometimes I go on MSN Messenger, and talk to PJ with some desperate problem I'm having with Operation Pong. I go through the steps, and then I realized how stupid I'd been. :wink:


OPEN file routine adding extra "0" to output file. - Dex4now - 03-09-2004

It isn't that the DO . . . LOOP was difficult, just that I was using it, along with incrementing the count variable, to simply perform the function thats built-in to LOF.

Its the fact of how long I spent trying to get rid of the extra "zero", when the LOF function would have solved the problem several hours ago. Posting the question in this forum was the very tail-end of the process . . .

. . . but well worth the effort. Now, I'll never forget it again. 8)


OPEN file routine adding extra "0" to output file. - KiZ - 03-09-2004

Or couldnt you just swap the GET and PUT statements? Wink