Posts: 63
Threads: 10
Joined: Jan 2003
Quote:Or couldnt you just swap the GET and PUT statements?
But wouldn't that cause the first run through the loop to PUT a zero at the beginning, rather than the end of the file? Their wouldn't be any data in a$ yet, and the two files need to end up being identical.
Posts: 2,771
Threads: 96
Joined: Oct 2003
Yea... I realised after I posted =P
but how about
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)
IF count THEN PUT #2, count, a$
count = count + 1
GET #1, count, a$
LOOP
CLOSE
SYSTEM
I havent tested but i reckon it would work?
Posts: 6,419
Threads: 74
Joined: Mar 2002
Code:
SUB copy (f1$, f2$)
f1% = FREEFILE
OPEN f1$ FOR BINARY AS #f1%
f2% = FREEFILE
OPEN f2$ FOR BINARY AS #f2%
WHILE NOT EOF(f1%)
buffer$ = INPUT$(16384, f1%)
PUT #f2%, , buffer$
WEND
CLOSE f1%, f2%
END SUB
Posts: 3,616
Threads: 287
Joined: Jan 2003
Yes..I was about to post about that method. File copying is faster when you copy in chunks. :wink:
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 63
Threads: 10
Joined: Jan 2003
Thanks Nathan for reminding me about using a buffer.
It worked perfectly, (and fast).
Let me ask one more thing related to this.
If I know ahead of time that my file will always be smaller than
16k, can I skip the WHILE . . . WEND loop, and just do a direct:
Code:
OPEN f1$ FOR BINARY AS #1
OPEN f2$ FOR BINARY AS #2
buffer$ = INPUT$(16384, 1)
PUT #2, , buffer$
CLOSE #1, #2
BTW - I tested that code, and it seems to work, just wondering if there were any negative reasons to doing it that way.
Dex
Posts: 3,522
Threads: 189
Joined: Dec 2003
The only negative thing about using a buffer is that when you'r code increase in size the buffer MIGHT generate a "out of memory" error.
I abuffer of 4096 is usually enough. And you won't run out of memory as soon.
Then again, as long as it works, go with it =)
Posts: 3,616
Threads: 287
Joined: Jan 2003
If you try to get a chunk that is larger than the file itself, that's fine - it won't crash, and it will be just as if you got a properly-sized, small enough chunk. So no problems should occur with this:
Code:
OPEN "file1.dat" FOR BINARY AS #1
OPEN "file2.dat" FOR BINARY AS #2
DO
buffer$=INPUT$(16384,#1)
IF EOF(1) THEN EXIT DO
PUT #2,buffer$
LOOP
CLOSE #1,#2
That will work, I believe, even if file1.dat is 10 bytes.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 63
Threads: 10
Joined: Jan 2003
Thanks,
This is part of a small utility I'm writing that modifies the AUTOEXEC.BAT file, so, 4096 should work fine. (Or maybe 8192)
I can't imagine anyone having an AUTOEXEC.BAT file larger than that. (Although, its possible.)
Dex