Announcement

Collapse
No announcement yet.

Open File for Random or Binary

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Open File for Random or Binary

    I am having a small problem with files opened for Binary or for Random, that I believe the code below is showing me that if the file does not exist, then it creates the file (like Append would), and because I do not write anything to the file, I now have a file in my directory with 0 Kb.

    (thats if I understand it correctly, but found nothing in the help files to verify this).

    My problem is, that I open a file from a subdirectory, and read the information, and then close the file. But now I have a new 0Kb file in my main directory.

    Is there a way around this? or do I have to go back to Open FileName for Input? so that a "Blank Copy of the File" is not created?

    I would like to use Binary, because if I use "For Input" the help file says
    UDT variables may not be used
    This should be a simple fix, or if I have to kill the 0Kb file as a work around I could do that too. But hoping someone has a better idea?

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
         LOCAL FileName AS STRING
         LOCAL  Index         AS LONG
         LOCAL  FileNo        AS LONG
         LOCAL sFileBuffer AS STRING
    
                  FileNo = FREEFILE                                 'Open database
    FileName = "03f0 Hewlett Packard.db"
    '          OPEN FileName FOR BINARY AS FileNo                'Get database into string
              OPEN FileName FOR RANDOM AS FileNo                'Get database into string
                   GET$ FileNo, LOF(FileNo), sFileBuffer        'Close database
              CLOSE FileNo
    
    
    END FUNCTION
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Look up OPEN [..] ACCESS READ in the help file under the OPEN statement. An error should be generated if the file does not exist. Alternatively, you could check for it's existance using DIR$ or FindFirstFile before opening the file.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      >thats if I understand it correctly, but found nothing in the help files to verify this).

      Look Harder. I found this under OPEN (PB/Win 8.03 help)
      If you try to open a nonexistent file for OUTPUT, APPEND, RANDOM, or BINARY operations, a new file is automatically created.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Thanx Kev and Michael,
        You both verified what I thought, and found the information on the "OPEN statement" page. I was looking at the "Binary Files" page.

        I also discovered another mistake on my part (should've caught if I had looked closer at the help files) that in reality what I was doing with my real code was.
        1. Check if File exists? (I had "Usb Database\FileName" in this case)
        2. Open the File as Binary (I goofed and had just "FileName") here
        3. Read in the data and parse it.


        And what was happening (and I did not think about it correctly till now was)
        1. Check if File exists? (Yep it exists)
        2. Open the File as Binary (I goofed and had just "FileName" so in turn a new file was created with the same name but a different folder)
        3. Read in the data and parse it. (Nothing to read in new file, so nothing to parse.)


        Thanks again for the help. I knew it had to be something simple, and I was just missing what it was
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          If the data are really a UDT, there is no need to parse it after loading it as a string.

          See TYPE SET in your help file and the easy way will be pretty obvious to you.

          For that matter, if you open RANDOM or BINARY, you can GET directly into a UDT.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment

          Working...
          X