Results 1 to 13 of 13

Thread: Error opening/reading file??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Error opening/reading file??

    The following code attempted to open and read a nonexistent file, but did not act the way I thought it would. The intent is to generate the file if it does not already exist. I expected the On Error to direct Control Flow to FileError, but the bolded MsgBox Statement executed.
    VB Code:
    1. On Error GoTo FileError
    2.  
    3. SuitFileNumber = FreeFile
    4. Open File_Name For Binary Access Read Lock Write As SuitFileNumber
    5. Get SuitFileNumber, 1, InputStamp
    6.  
    7. If InputStamp.ProjectName = FileStamp.ProjectName _
    8.         And InputStamp.AuthorFirst = FileStamp.AuthorFirst _
    9.         And InputStamp.AuthorLast = FileStamp.AuthorLast _
    10.         And InputStamp.RecordCount = 512 Then
    11.         'Fall through and read file'
    12.     Else
    13.         [b]Junk = MsgBox("File corrupted or not Suit Data File")[/b]
    14.         Close SuitFileNumber
    15.         ThereIsSuitData = False
    16.         Exit Function
    17.   End If
    18. [b]. . . [/b]
    19. FileError:
    20. [b]. . .[/b]
    The above code is in a Function. I have included only the code which seems pertinent. File_Name is passed to the Function as parameter and seems to be syntactically correct.

    App.Path & "SuitData.qqq" was used, and Debug analysis indicated that it became the name of a nonexistent file.

    I expected the Open to fail, causing Control Flow to go to FileError. The input area looks like uninitialized data, indicating that no I/O actually occurred.

    Why did neither the Open nor the Get cause control flow to go to FileError?

    BTW: Due to the way some mainframes react, I always supply a place for values returned from Functions. Hence: Junk = MsgBox.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    App.Path & "SuitData.qqq"
    If that's what you use then change it to App.Path & "\SuitData.qqq"
    McGenius

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    McGenius: The BackSlash (\) was supplied as part of App.Path.

    My problem is not the syntax. I do not understand why the On Error does not seem to function as expected. I expected it to cause control Flow to go to FileError due to the attempt to Open & Get from a nonexistent file.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Hmm ...
    First: App.Path does not supply last backslash unless you did
    Second: what exactly are you expecting to fail ??? Error hanlder won't be triggered just because InputStamp.ProjectName <> FileStamp.ProjectName - it must be a real time error such "Invalid file name ..." or so to occur, otherwise it's an errorless flow.
    McGenius

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by McGenius
    Hmm ...
    First: App.Path does not supply last backslash unless you did
    Second: what exactly are you expecting to fail ??? Error hanlder won't be triggered just because InputStamp.ProjectName <> FileStamp.ProjectName - it must be a real time error such "Invalid file name ..." or so to occur, otherwise it's an errorless flow.
    App.Path supplies the backslash if your app is loaded from the root of a drive.... and if it's loaded from a net folder ("\\computername\folder\")
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Guv...

    Code:
    Open File_Name For Binary Access Read Lock Write As SuitFileNumber
    When a file is opened as Binary, Random, Append, or Output, the file is created if it doesn't already exist. An Open will only fail if the file is opened for Input. Since it is now a zero byte file, the Get attempt will have zero bytes to read, hence the uninitialized data area.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by Mc Brain
    App.Path supplies the backslash if your app is loaded from the root of a drive.... and if it's loaded from a net folder ("\\computername\folder\")
    That isn't always true.
    McGenius

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Save your project to the root of your hardrive and ask for the App.Path.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    For some reason, Mc Brain you're always looking at immediate result HOWEVER if you read my previous post one more time you'll see 2 words: isn't always ... VB's App object is very problematic and is known for years as very unstable one.
    McGenius

  11. #11
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I'm not saying you were wrong. But, if you try to run your app from a CD Drive (let's say D:), the App.Path would return the backslash. You have to code your app so that this slash won't "hurt" you.

    Edit: f***n' smiles
    Last edited by Mc Brain; Apr 19th, 2003 at 09:58 PM.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  12. #12
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by Mc Brain
    ... You have to code your app so that this slash won't "hurt" you.
    Oh, that's outta question ...
    McGenius

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    CrptcBlade:Thanx!! You were correct: Open Binary created an empty file. The Get could read no data, but did not fail since the file existed.

    This forum has yet to fail me.

    BTW: It seems strange that Open for input would create a file instead of complaining about it being nonexistent.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width