Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

accessing external files?

UnrealDangerUnrealDanger Member Posts: 9
Right now my program is on a floppy disk and accesses files on the same disk, so OPEN "A:FILEFOLDERFILE.TXT" FOR INPUT AS #1 works fine. But when I'm done I want to burn this onto cd's to distribute at work. Normally I would change the A: to D:, but what if the cdrom drive isn't D? On my setup for instance my cdroms are E and F. Can the OPEN statement use current directory or is there a way for the program to know what drive it's on?

Comments

  • brisraybrisray Member Posts: 60
    The OPEN command will use the current drive and directory if none is specified. Unfortunately this is going to be the directory where QBasic.exe is, not the program. Which isn't what you want.

    Someone asked a similar question at
    http://www.tek-tips.com/gviewthread.cfm/pid/314/qid/198826

    Nearly all the replies he got came back with code involving interupts, something I know next to nothing about. QBasic (esp. v1.1 which is what I use) has very little support for getting which disks are available on a computer except through these interupts.

    You may have to ask the user what drive the CD is in and use that as a variable in the OPEN statement.

    One way I did think of was to use the following. This will work if you know which directory the file is in, as you're writing the CD you'll know this anyway.

    CLS
    ON ERROR GOTO errtrap
    FOR Count = ASC("A") TO ASC("G")
    Flag = 0
    FileDir$ = CHR$(Count) + ":myfoldermyfile.txt"
    OPEN FileDir$ FOR INPUT ACCESS READ LOCK READ WRITE AS #1
    IF Flag = 0 THEN PRINT "File found on "; CHR$(Count)
    NEXT Count
    END

    errtrap:
    PRINT ERR, CHR$(Count) 'I put this in to see what the error code was
    Flag = 1
    RESUME NEXT

    Not the most elegant way of doing it but it works. If the file was found then Flag = 0 inside the For loop, so you can do what you like with it once the program has found it.

    Ray
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : The OPEN command will use the current drive and directory if none is specified. Unfortunately this is going to be the directory where QBasic.exe is, not the program. Which isn't what you want.
    :
    : Someone asked a similar question at
    : http://www.tek-tips.com/gviewthread.cfm/pid/314/qid/198826
    :
    : Nearly all the replies he got came back with code involving interupts, something I know next to nothing about. QBasic (esp. v1.1 which is what I use) has very little support for getting which disks are available on a computer except through these interupts.
    :
    : You may have to ask the user what drive the CD is in and use that as a variable in the OPEN statement.
    :
    : One way I did think of was to use the following. This will work if you know which directory the file is in, as you're writing the CD you'll know this anyway.
    :
    : CLS
    : ON ERROR GOTO errtrap
    : FOR Count = ASC("A") TO ASC("G")
    : Flag = 0
    : FileDir$ = CHR$(Count) + ":myfoldermyfile.txt"
    : OPEN FileDir$ FOR INPUT ACCESS READ LOCK READ WRITE AS #1
    : IF Flag = 0 THEN PRINT "File found on "; CHR$(Count)
    : NEXT Count
    : END
    :
    : errtrap:
    : PRINT ERR, CHR$(Count) 'I put this in to see what the error code was
    : Flag = 1
    : RESUME NEXT
    :
    : Not the most elegant way of doing it but it works. If the file was found then Flag = 0 inside the For loop, so you can do what you like with it once the program has found it.
    :
    : Ray
    :

    Actually, the relative path will be the folder from which your exe was started, not the folder the exe is in and not the folder qbasic is in. Unless, of course, you issue a command that changes the current folder such as ChDir. QB and VBDOS are woefully inadequate when it comes to file handling and there's not much you can do without interupts, upgrading or moving to a different language or environment.

    c:>cd windows

    c:windows>..qbappmyapp.exe

    A relative path you use will be parsed relative to c:windows and not c:qbappmyapp.exe

    When running inside the QB environment, the paths will be parsed relative to qb's path.

    One option is to use Ray's solution to find your app's folder and then store the drive in a variable. You'll need to use all drives up to Z.

    Just my two cents...
  • citpescitpes Member Posts: 18
    I had the same problem once. If you compile your program into a exe then the OPEN statement will open the directory that the exe is in. So if you are opening the directory 'a:datastuff.dat' then before you compile change that to
    'datastuff.dat'.

    Here is what I did doing my programs development to make is easier:

    path$="c:myprogfilesdata"

    OPEN path$ + "stuff.dat" for whatever AS #1
    *do stuff*
    CLOSE #1

    Then when I compiled I made path$="". Worked like a charm!
  • UnrealDangerUnrealDanger Member Posts: 9
    Your example code showed me exactly how to do this. The example along with the others for this thread helped me learn how to add this to my program. But I have one more question...When it checks drives, and no disk is present in A, or any drive for B for that matter it asks for a disk. That is going to stop the function from being automatic. Is there a way to ECHO a no disk response from a drive? Thanks for the great answers so far!
  • brisraybrisray Member Posts: 60
    : Your example code showed me exactly how to do this. The example along with the others for this thread helped me learn how to add this to my program. But I have one more question...When it checks drives, and no disk is present in A, or any drive for B for that matter it asks for a disk. That is going to stop the function from being automatic. Is there a way to ECHO a no disk response from a drive? Thanks for the great answers so far!
    :

    The problem is that the replies to your original post refer to different versions of QBasic, the versions most people use are 1.1, 4.5 or 7.1

    I can't speak for the other versions but v1.1, the version I use, cannot produce .EXE files so it returns the path to QBasic.exe. The code I gave for this returns an error code, which is trapped by the program, so will not ask for a disk.

    The error codes (on my machine) are 71 (Disk not ready) for A: without a disk, 53 (File not found) for A: with the wrong disk and 76 (Path not found) for B: - I don't have a B: drive.

    Ray
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ________ < http://forcoder.org /> free video tutorials and ebooks about \ Go, C++, Assembly, JavaScript, PL/SQL, Delphi, C, Visual Basic .NET, Swift, Perl, C#, Ruby, MATLAB, Java, R, Visual Basic, Python, Scratch, Objective-C, PHP VBScript, ML, Clojure, Scheme, Lisp, Julia, SAS, Apex, ABAP, COBOL, Awk, Logo, F#, Fortran, Transact-SQL, Ada, Lua, FoxPro, Dart, LabVIEW, Bash, Scala, Alice, Kotlin, Rust, Erlang, Hack, D, Crystal, Prolog \ ____

Sign In or Register to comment.