MapInfo Pro Developers User Group

 View Only
  • 1.  MapBasic Open File command no longer working

    Posted 03-28-2023 13:06
      |   view attached

    Hi all,

    I recently upgraded to v2021.1 from v2019. I have a MapBasic program I wrote last year that worked fine, but now MI keeps on choking on the Open File command in the program. Has something changed with MapBasic language that there has been a change in how it handles the Open File command? I have looked in the reference book and everything looks the same. Someone mentioned that with MI v2021 there was a change so that you have to specify an absolute path.

    Here is the code (stripped down to the relevant commands):

    include "mapbasic.def"

    Print "Writing test file"
    Open file "test.txt" for output as #1

    Print #1, "Hi"

    Close File #1

    This worked fine in MI v2019 and I have made no changes to the program code. I now get a error that it Cannot Open File "test.txt". 



    ------------------------------
    Sam Wetsel
    Anterix Inc
    Woodland Park NJ
    ------------------------------


  • 2.  RE: MapBasic Open File command no longer working

    Posted 03-29-2023 01:45

    I have already answered that question on MI-L. Wasn't the answer good enough?



    ------------------------------
    Uffe Kousgaard
    ROUTEWARE
    Roskilde
    ------------------------------



  • 3.  RE: MapBasic Open File command no longer working

    Employee
    Posted 03-29-2023 03:17

    Hi Uffe,

    I think the question was posted simultaneously to MapInfo-L and to this community. 

    For the record and for those not on MapInfo-L, here's what Uffe replied:

    In MapInfo 2021 you need an absolute path for your files, you can not rely on just "test.txt".
    MapInfo must be making changes to "current directory" behind the scenes.



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 4.  RE: MapBasic Open File command no longer working

    Posted 03-29-2023 03:39

    No, it was posted 12 hours after my reply. I actually checked :-)



    ------------------------------
    Uffe Kousgaard
    ROUTEWARE
    Roskilde
    ------------------------------



  • 5.  RE: MapBasic Open File command no longer working

    Employee
    Posted 03-29-2023 03:44

    And I didn't, as you can tell :-D



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 6.  RE: MapBasic Open File command no longer working

    Posted 03-29-2023 19:51
    Edited by Sam Wetsel 03-30-2023 11:49

    Yes, your answer gave me direction and I appreciate it, but I still have questions about absolute path and how I would do that.

    After searching the Precisely knowledge base and not finding this addressed anywhere, I decided it was better to post this question in this forum so that Precisely employees could see it. I find it concerning that Precisely has modified a significant function in the way MapBasic works with regards to any file interaction requiring input or output functions without notifying users of this change. This makes all of my MB programs no longer backwards compatible in MI 2021 if they use input or output (or in other functions that I am not aware of that have been affected). There is nothing in the manual under the the Open File statement to this effect; in fact, it appears to be the same language it has been forever (see below). I have tried the Set Path command, but that doesn't work. I can't just hard code a path in every Open File command as my programs are used in different environments with different directory structures, so I don't know how to modify my programs to work correctly.

    I'm sorry, I didn't mean to offend you by posting here. I just had a time restriction on getting this fixed.



    ------------------------------
    Sam Wetsel
    Anterix Inc
    Woodland Park NJ
    ------------------------------



  • 7.  RE: MapBasic Open File command no longer working

    Posted 03-30-2023 01:40

    The issue is as a developer we should never rely on current directory, because it is a global thing in Windows and it may be changed by any other process at any point. But nevertheless we do it all the time, hoping it goes OK. Which it usually does.

    But here are two functions, which you can use, so you don't need to hardcode the actual paths:

    ApplicationDirectory$()

    PathToDirectory$(TableInfo(tablename,TAB_INFO_TABFILE))



    ------------------------------
    Uffe Kousgaard
    ROUTEWARE
    Roskilde
    ------------------------------



  • 8.  RE: MapBasic Open File command no longer working

    Employee
    Posted 03-30-2023 01:40
    Edited by Peter Møller 03-30-2023 01:41

    Sam,

    You are right. We should certainly get that change documented, and also change the examples shown under Open File.

    There are a few ways to deal with paths through MapBasic. It depends on what you are saving, or where.

    Here are a few ways to get to paths in MapBasic to avoid hardcoding them (which really should be avoided):

    • ApplicationDirectory$(): Return the path/folder where your applications resided
    • PathToDirectory$(sFile): Return the path/folder from a given file
    • TempFileName$(""): Returns a temporary file name in a given path/folder. "" will use the System Temp folder.
    • GetFolderPath$( folder_id ): returns the path to various locations on your system, like My Docs, My Pics, App Data, and MapInfo Preference folder.

    These are just a few ways to get to folders.

    For a temporary folder, I often do this: 

    sTempFolder = PathToDirectory$(TempFileName$(""))

    I can now add the variable sTempFolder to all my temporary file names:

    Open File sTempFolder & "test.txt" For Output As #123

    You can also use FileSaveAsDlg() and FileOpenDlg() to ask the user where to save or open data.

    And finally, you can read a path from a configuration file.



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------