file thing

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
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

file thing

Post by Seb McClouth »

Hey

I need to do some file stuff...

I need to create a new file... but if it exists... I want to add a new line...

I don't have anything this far yet...

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Code: Select all

open "file" for append as #1
I have left this dump.
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth »

How can someone check if the file is new or existing?
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Seb McClouth wrote:How can someone check if the file is new or existing?
DIR$()
Or just check if the file is 0 bytes long.. Does it really matter?
I have left this dump.
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 »

bummer i hate not being at home(2 more weeks)


ask MONEO he made me a peice of code for
our DEEBEE program that checks for an
existing file...worked great!....
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth »

it matters because the program is deleting a file and recreating it... for that reason I need.

Thx Sid I'll ask him.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

**********************************************************
Hey Moneo
Hows you? Me doing great.
I have question and Sid6.7 told me you might be some help.
For a program I need to remove a file, create a new one, and add lines to the file.
But the adding thing is kinda blury, because first I need a check to see if the file is empty or already contains some text. If it does contain text I need to put the new text after the old one.
You think you can help me on this one?
grtz
Seb
**********************************************************
Hi Seb,
I got your above message on PM. I'm posting my reply here because other people have already posted stuff.

In your first post you said: "I need to create a new file... but if it exists... I want to add a new line..."

In your last post you also said: "it matters because the program is deleting a file and recreating it... for that reason I need."

Sid6.7 says I helped on the DEEBEE program to check for an existing file. I checked this program and there's no "file exist" logic there. I wrote a subroutine to "delete" a file by opening it for output and closing it, which makes it a zero-length-file.

Anyway, these are the operations that I understand you want to perform:

1) REMOVE (OR DELETE) A FILE.
If you know the filename and you definitely want to delete it, whether it exists or not, do this:
OPEN "file" FOR OUTPUT AS #9
CLOSE #9
KILL "file"

The OPEN FOR OUTPUT makes it a zero-length-file, which exists for the KILL.

2) CREATE A NEW FILE.
If you definitely want to create this file as a new file, whether it already exists of not, and whether it already has data on it or not, do this:
OPEN "file" FOR OUTPUT AS #9

3) ADD LINES TO AN EXISTING FILE.
If you want to add lines to an existing file, whether or not the file already exists, do this:
OPEN "file" FOR APPEND AS #9

Except for the DELETE, you could probably use the OPEN FOR APPEND for all the other cases. I think that's what Z!re was trying to tell you.

In summary, Based on what you say you want to do, I don't think you need to make any decision regarding whether the file exists or not. File exist logic can get hairy, and the only good way to do it is to use assembler logic.

Hope that helps..... Regards..... Moneo
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth »

thx everybody...
Moneo you explaining did the trick. I've implented the code and it works just like I wanted it too.

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
Post Reply