Multiple files appending

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
cryptid
Newbie
Posts: 9
Joined: Thu Aug 18, 2005 2:57 am
Contact:

Multiple files appending

Post by cryptid »

Well i have to edit multiple files situated in diffrent locations in the computer i know it can be done using the command [open "C:\file.txt" for append as #1] but my problem is that i have to append the same lines in the other files also so is there a way that i can append the same lines at a time to all the files [i am a lazy person i dont like to type much & any way i need my program to be small and fast] so is there a way to open multiple files and append all of the at once i tried doing thins but it didnt work

open "C:\file.txt" for append as #1
open "D:\file123.txt" for append as #1
Print #1, "hi Blha Blha BLHA"
close #1

but the ompiler returned some error
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Code: Select all

stri$ = "Hello, World!"
open "C:\file.txt" for append as #1
open "D:\file123.txt" for append as #2
Print #1, stri$
Print #2, stri$
close #1 , #2
Or:

Code: Select all

dim flist(5) as string

flist(0) = "C:\file.txt"
flist(1) = "C:\blarga"
flist(2) = "C:\poof\tjoff"
flist(3) = "C:\Yadda"
flist(4) = "C:\random\random\whatever\file.txt"
flist(5) = "D:\otherhd\blipp"

stri$ = "Hello, World!"

for a = 0 to 5
  open flist(a) for append as #1
  Print #1, stri$
  close #1
next
I would recomend the second way..
I have left this dump.
User avatar
Xerol
Veteran
Posts: 81
Joined: Tue Jan 04, 2005 6:27 pm
Location: Timonium, MD
Contact:

Post by Xerol »

I think this works better:

Code: Select all

dim flist(6) as string 

flist(1) = "C:\file.txt" 
flist(2) = "C:\blarga" 
flist(3) = "C:\poof\tjoff" 
flist(4) = "C:\Yadda" 
flist(5) = "C:\random\random\whatever\file.txt" 
flist(6) = "D:\otherhd\blipp" 

stri$ = "Hello, World!" 

for a = 1 to 6
  open flist(a) for append as #a
next a

for a = 1 to 6
  Print #a, stri$ 
next a

'do other stuff here. you can post to all the files as much as you want using the above method

for a = 1 to 6
  close #a
next a
I use it a lot. I don't think many people know that you can use a variable after the # when referring to files.

Image
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.

Xerol's Music - Updated Regularly!
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

You should use another array and store the file id in it, to get a valid fileid use FREEFILE.

But, your way will fail, as you can only open ~16 or so files, as specified in the config.sys file (iirc)

Mine is set to 32 for example..


The way I posted does the same thing, using less memory and less resources overall, it's always a good idea to keep as few files open as possible..
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} »

Z!re wrote:You should use another array and store the file id in it, to get a valid fileid use FREEFILE.

But, your way will fail, as you can only open ~16 or so files, as specified in the config.sys file (iirc)

Mine is set to 32 for example..


The way I posted does the same thing, using less memory and less resources overall, it's always a good idea to keep as few files open as possible..
you took the words straight out of my mouth.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Right, Z!re's method is much simpler, easy to understand, and certainly will get the job done for ant number of files with no complications.

Nice work, Z!re! :D

BTW, Zerol, in your version, you don't need the FOR loop at the end to close the files. Just a simple CLOSE with no parameters will close all the files. As a matter of fact, unless the program is going to open any other files afterwards, you can just do an END or SYSTEM which will also close all the files, or code nothing at the end of the code, which does this by default.
*****
Last edited by moneo on Thu Sep 29, 2005 7:04 pm, edited 1 time in total.
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Thank you moneo =)
I have left this dump.
Post Reply