Opening 2 files at the same time

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Seb McClouth

Opening 2 files at the same time

Post by Seb McClouth »

I never done this before and would like to know how this can be achieved.

It needs to use these:
  • Code: Select all

    value = FREEFILE

    Code: Select all

    OPEN FileName$ FOR xxxx (what should be here again?) AS value
    DO
    Input value, temp$
    IF left$(TEMP$, 12) ="needed_value" then FileName$2=right$(Temp$,len(Temp$-13)
    LOOP until EOF(1)
    close Value

    Code: Select all

    OPEN FileName$2 FOR APPEND AS value
    Print value, "Sumfin"
    close value
grtz
Seb
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Not sure what you mean by using those codes, but to open 2 files at once:

Code: Select all

OPEN "File1.txt" FOR INPUT AS #1 'Open File 1
OPEN "File2.txt FOR OUTPUT AS #2 'Open File 2

INPUT #1, Stuff
PRINT #2, Stuff

CLOSE #1
CLOSE #2
That will open 2 files at once, and in this code read from 1 and rewrite into 2..

:wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Seb McClouth

Post by Seb McClouth »

What I mean is that I have to open the first file, search into it for a certain string, e.g.: standardlog = etc/var/boot.log.
Then retrieve that line, retrieve "etc/var/boot.log" from it and then open "etc/var/boot.log"

grtz
Seb
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Re: Opening 2 files at the same time

Post by Rattrapmax6 »

Okay.. then I believe this is how you'd go about that... :wink:

Code: Select all

f% = FREEFILE

OPEN FileName$ FOR INPUT AS #f%
DO
INPUT #f%, temp$
IF LEFT$(TEMP$, 12) ="needed_value" THEN 
    FileName2$ = RIGHT$(Temp$, LEN(Temp$-13)
END IF
LOOP UNTIL EOF(1)
CLOSE #f%

OPEN FileName2$ FOR APPEND AS #f%
PRINT #f%, "Sumfin"
CLOSE #f%
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
mennonite

ah, okay two at once

Post by mennonite »

'using freefile with two open files at once:

file01$="somefin"
file02$="somefine.lse"

f=freefile
open file01$ for input as f
g=freefile
open file02$ for output as g

do
if not eof(f) then line input #f, q$ 'load from file01$
print #g, q$ 'save to file02$
loop until eof(f)

close #g
close #f

i've never seen a well coded program that required freefile, but it is a cute function all the same. i wish there was an analog for the SCREEN command. (not the SCREEN function.)
mennonite

on second thought

Post by mennonite »

the above code doesn't do anything besides combine both of rattrapmax6's examples, now that i've read them. i wasn't paying attention to anything except whether they show how to open two files at once using freefile, which technically, neither do. although it was better to redo your own code with freefile, which has been done already :)

i still maintain that i've never seen a well coded program that needs freefile, but that's not to say such a program doesn't exist. at first it looks like a useful function, but i can't guess how it would be. maybe someone can explain that to me sometime, using a *real* example, a "i needed it because" example, instead of the obvious "maybe it would be useful this way" example, the only kind i could make for it. or maybe it becomes very, very useful with subs, only i think it would be just as easily simulated with a global variable? oh well...
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

FreeFile allows you to view a file as a whole right? Like for BMP loaders, or viewing the rambled stuff in a EXE with out mesing it up.... but normal text files wouldn't really need it..

correct me if I'm wrong...
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

FREEFILE retuns the next free file ID number..

As you cant open two files with the same ID, you use FREEFILE, and it'll always work.. no compatibility issues.
I have left this dump.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

What would it do if you already had 16 files open? (I think 16 is the max, correct me if I am wronge)
Image
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

lol allow me to correct you...;-)...or rather extend your knowledge ;-)

The number of files you can open is as determined by the files statement in your configuration (files = 255 would give you 255 opened files. default is 20)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

QB maximizes the amount of open files to 255, even if you change it to 4096 in config.sys, qb still wont open them.

There's no documentation on what happens if FREEFILE fail, but I guess, since it's QB: Illegal function call
I have left this dump.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Yup... damn QB errors...
Image
Post Reply