Results 1 to 32 of 32

Thread: [RESOLVED] I need help reading a data file. (Visual Basic 6.0)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Resolved [RESOLVED] I need help reading a data file. (Visual Basic 6.0)

    I'm having trouble reading a file. The file is a core file from Microsoft's now debunked but open source game Allegiance that was written in C/C++; I don't know C/C++. (On a side note: A few users kept the game alive and running, see http://www.freeallegiance.org/forums/index.php?act=home )

    I have tried:

    VB Code:
    1. Dim strText As String
    2.     Dim intFile   As Integer
    3.  
    4.     intFile = FreeFile()
    5.  
    6.     Open App.Path + "\dn_000450.igc" For Input As #intFile
    7.         MsgBox (LOF(intFile))
    8.         MsgBox (EOF(intFile))
    9.         strText = Input$(LOF(intFile), intFile)
    10.     Close #intFile
    But it gives an error.

    I have also tried:
    VB Code:
    1. Dim sfile As String
    2. sfile = App.Path + "\dn_000450.igc"
    3. Dim myArray() As String
    4. ReDim myArray(0)
    5. Open sfile For Input As #1
    6. Do Until EOF(1)
    7.    Input #1, myArray(UBound(myArray))
    8.    ReDim Preserve myArray(UBound(myArray) + 1)
    9. Loop
    10. ReDim Preserve myArray(UBound(myArray) - 1)
    11. Close #1
    12. 'Check to see if everything was read
    13. For i = 0 To UBound(myArray())
    14. txt1.Text = txt1.Text & myArray(i)
    15. Next i
    But it only appeared to read part of the file.

    How do I read the file and extract usable data from it??


    Additional information:

    Currently I'm reading the .CSV file output from a program called CoreDump that was written in Python, but the current version is buggy and doesn’t output everything. Currently, I don't know any Python and would like to deal with just the one core file instead of the several CSV files that CoreDump outputs.

    If anyone wants me to, I will post the Python source code to CoreDump.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: I need help reading a data file. (Visual Basic 6.0)

    First of all, this is a binary file so you need to treat it as such when reading/writing to and from it.

    ie, to load the data into memory:

    VB Code:
    1. Dim bytData() As Byte, strPath As String
    2. Dim strData As String
    3.  
    4. strPath = "C:\dn_000450.igc"
    5.  
    6. ReDim bytData(1 To FileLen(strPath)) As Byte
    7.  
    8. Open "C:\dn_000450.igc" For Binary Access Read As #1
    9. Get #1, , bytData()
    10. Close #1
    11.  
    12. strData = StrConv(bytData(), vbUnicode)
    13. Erase bytData()
    14.  
    15. MsgBox Len(strData) & " bytes read of " & FileLen(strPath)

    Also, this file should be read into a data structure, as it was written. ie:

    VB Code:
    1. Type FileData
    2.     strWeapon As String
    3.     sinRange As Single
    4.     '...
    5. End Type
    6.  
    7. Dim udtData() As FileData, strPath As String
    8.  
    9. strPath = "C:\dn_000450.igc"
    10.  
    11. Open "C:\dn_000450.igc" For Binary Access Read As #1
    12. Get #1, , udtData()
    13. Close #1
    14.  
    15. Debug.Print udtData(0).Weapon
    16. Debug.Print udtData(1).Weapon
    17. '...

    But in order for this to work, you need to know what data structure they used, so you will need to find some sort of documentation on it.

    A good way to know if it was written with a data structure is by looking for certain patterns in the file, as shown in the screenshot.
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by DigiRev
    Also, this file should be read into a data structure, as it was written. ie:

    VB Code:
    1. Type FileData
    2.     strWeapon As String
    3.     sinRange As Single
    4.     '...
    5. End Type
    6.  
    7. Dim udtData() As FileData, strPath As String
    8.  
    9. strPath = "C:\dn_000450.igc"
    10.  
    11. Open "C:\dn_000450.igc" For Binary Access Read As #1
    12. Get #1, , udtData()
    13. Close #1
    14.  
    15. Debug.Print udtData(0).Weapon
    16. Debug.Print udtData(1).Weapon
    17. '...
    It gave me an error when I used an array, so I tried the following and it didn't give me an error, but it didn't print anything:

    VB Code:
    1. Type FileData
    2.     tech As String
    3. End Type
    4.  
    5. Dim udtData() As FileData, strPath As String
    6.  
    7. strPath = "C:\dn_000450.igc"
    8.  
    9. Open "C:\dn_000450.igc" For Binary Access Read As #1
    10. Get #1, , tech
    11. Close #1
    12.  
    13. Debug.Print udtData.tech
    14.  
    15. '...

    How to I get it to a readable form?

    But in order for this to work, you need to know what data structure they used, so you will need to find some sort of documentation on it.

    A good way to know if it was written with a data structure is by looking for certain patterns in the file, as shown in the screenshot.
    As I said before, I have the source code to the game, but it is written in C++; I don't know C++ and don't have any idea what to look for.
    The source code to the game can be viewed online at http://svn.alleg.net:8080/svn/Allegiance/trunk/src/ .
    The part of the code that specifically deals with the core format can be viewed at http://svn.alleg.net:8080/svn/Allegiance/trunk/src/Igc/ .


    I have also attached the Python source code to CoreDump which reads the file and outputs the variables into several .CSV files.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: I need help reading a data file. (Visual Basic 6.0)

    You need the code that holds the data structure for all the info in the file: weapons, ranges, etc...

    I don't have time to look through all of those source code files.

    Type structures in C look like this:

    Code:
    struct DataElement {
           string SVal;
           int    iVal;
           bool   hasData;
    }
    as opposed to VB...

    VB Code:
    1. Type DataElement
    2.     SVal As String
    3.     iVal As Integer
    4.     hasData As Boolean
    5. End Type

    If you have the source on your computer, you could upload a zip file of the whole project and that would make it a lot quicker to find it.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    The entire source is 58.5 MB. Their knowledgebase states how to dl the entire code.
    http://www.freeallegiance.org/knowle...e/entry/13/45/
    Thalgor has graciously set up a Subversion repository on one of his servers to allow all Allegiance developers to collaboratively develop additions and enhancements to Allegiance.
    This repository is located at http://svn.alleg.net:8080/svn/Allegiance and can easily be browsed with your web browser.
    Subversion website: http://subversion.tigris.org/

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    I found
    Code:
    typedef short           ObjectID;
    and
    Code:
    typedef ObjectID        PartID;
    and
    Code:
    typedef char  Mount;
    and
    Code:
    struct  PartData
    {
        PartID          partID;
        short           amount;
        Mount           mountID;
    };
    What is the VB equivalent of typedef?

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by Tontow
    I found
    Code:
    typedef short           ObjectID;
    and
    Code:
    typedef ObjectID        PartID;
    and
    Code:
    typedef char  Mount;
    and
    Code:
    struct  PartData
    {
        PartID          partID;
        short           amount;
        Mount           mountID;
    };
    What is the VB equivalent of typedef?
    Here's a good page to read:

    http://www.vbthunder.com/articles/readcpp.php

    Scroll down to "Structures".

    I don't have a lot of time right now but maybe tomorrow I can help you out.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Thank you, that was a good read.

    But as far as I can find/tell Visual Basic only has the equivalent of "struct" and not:
    Code:
    typedef ObjectID        PartID;
    So my best guess for converting the following to Visual Basic:

    Code:
    struct  PartData
    {
        PartID          partID;
        short           amount;
        Mount           mountID;
    };
    Would translate into:
    VB Code:
    1. Type PartData
    2.       partID as Integer
    3.       amount as Integer
    4.       mountID as Byte
    5. end type
    Is this correct?

  9. #9
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by Tontow
    VB Code:
    1. Type PartData
    2.       partID as Integer
    3.       amount as Integer
    4.       mountID as Byte
    5. End type
    Is this correct?
    Yes

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Regarding recent discussion on typedef ObjectID, note the first line and SquadID.
    Code:
    typedef short           ObjectID;
    typedef ObjectID        BucketID;
    typedef ObjectID        PartID;
    typedef ObjectID        ShipID;
    typedef ObjectID        BuoyID;
    typedef ObjectID        AsteroidID;
    typedef ObjectID        HullID;
    typedef ObjectID        StationTypeID;
    typedef ObjectID        DroneTypeID;
    typedef ObjectID        DevelopmentID;
    typedef ObjectID        SectorID;
    typedef ObjectID        WarpID;
    typedef ObjectID        ProjectileTypeID;
    typedef ObjectID        TreasureID;
    typedef ObjectID        SideID;
    typedef ObjectID        StationID;
    typedef ObjectID        MissileID;
    typedef ObjectID        ChaffID;
    typedef ObjectID        MineID;
    typedef ObjectID        ProbeID;
    typedef ObjectID        ExpendableTypeID;
    typedef ObjectID        CivID;
    typedef ObjectID        MunitionID;
    typedef ObjectID        TreasureSetID;
    typedef int             SquadID;
    
    typedef ObjectID        WingID;
    predefining ObjectID just makes it easier to distinguish your C short variables from other C short variables (you can reference them by more descriptive 'ObjectID' rather than just saying its a short). And makes the 'description', for lack of a better term, available as a data type. what all that is saying is

    VB Code:
    1. Dim BucketI          As Integer
    2. Dim PartID           As Integer
    3. Dim ShipID           As Integer
    4. Dim BuoyID           As Integer
    5. Dim AsteroidID       As Integer
    6. Dim HullID           As Integer
    7. Dim StationTypeID    As Integer
    8. Dim DroneTypeID      As Integer
    9. Dim DevelopmentID    As Integer
    10. Dim SectorID         As Integer
    11. Dim WarpID           As Integer
    12. Dim ProjectileTypeID As Integer
    13. Dim TreasureID       As Integer
    14. Dim SideID           As Integer
    15. Dim StationID        As Integer
    16. Dim MissileID        As Integer
    17. Dim ChaffID          As Integer
    18. Dim MineID           As Integer
    19. Dim ProbeID          As Integer
    20. Dim ExpendableTypeID As Integer
    21. Dim CivID            As Integer
    22. Dim MunitionID       As Integer
    23. Dim TreasureSetID    As Integer
    24. Dim SquadID          As Long  'since it was an int in C
    25. Dim WingID           As Integer

    Since most of the declarations in igc.h would probably be used in the file, you can occupy yourself Tontow in the meantime with converting it into a vb module. just comment the classes for now.
    Last edited by leinad31; Oct 20th, 2006 at 02:03 PM.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Still can't find the procedures that access the file. Must have missed it due to reading all those lines

    If someone has the time, pls look for LoadIGCStaticCore function.
    Last edited by leinad31; Oct 20th, 2006 at 01:40 PM.

  12. #12
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: I need help reading a data file. (Visual Basic 6.0)

    Just a quick note: C int types are VB Longs, not Integers.

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    I based the conversion on the web page link they gave. It was defined as a short in C.

    EDIT: My previous explanation on ObjectID was confusing cause I said integer when I meant C short... I updated it.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by leinad31
    Still can't find the procedures that access the file. Must have missed it due to reading all those lines

    If someone has the time, pls look for LoadIGCStaticCore function.
    Maybe this will help?

    Starting at line 2819 in igc.h
    Code:
            //Warning these should never be called except from LoadIGCStaticCore
            virtual CstaticIGC*             GetStaticCore(void) const = 0;
            virtual void                    SetStaticCore(CstaticIGC*   pStatic) = 0;
    
            virtual short                   GetReplayCount(void) const = 0;
            virtual const char*             GetContextName(void) = 0;
    };
    
    class IbaseIGC : public IObject
    {
        public:
            virtual HRESULT         Initialize(ImissionIGC* pMission, Time now, const void* data, int length) = 0;
            virtual void            Terminate(void) = 0;
            virtual void            Update(Time   now)          {}
    
            //Exporting an object which doesn't support export is also bad.
            virtual int             Export(void*   data) const  { assert (false); return -1;}
    
            // GetUniqueID() is provided for convenience because AGC uses GetObjectType and GetObjectID often.
            virtual int             GetUniqueID(void) const     { return GetObjectType() | (GetObjectID() << 16); }
            virtual ObjectType      GetObjectType(void) const = 0;
    
            //Calling either of these methods on something that doesn't have either an object ID or a mission is bad.
            virtual ObjectID        GetObjectID(void) const     { assert (false); return NA;}
            virtual ImissionIGC*    GetMission(void) const      { assert (false); return NULL; }
    };
    And at line 5867 of igc.h (basically at the very bottom)
    Code:
    //------------------------------------------------------------------------------
    // normal igc files, i.e. missions can be dumped and loaded using these two
    // functions. They return true if successful.
    //------------------------------------------------------------------------------
    bool    DumpIGCFile (const char* name, ImissionIGC* pMission, __int64 iMaskExportTypes, 
                         void (*munge)(int size, char* data) = NULL);
    bool    LoadIGCFile (const char* name, ImissionIGC* pMission, void (*munge)(int size, char* data) = NULL);
    
    //------------------------------------------------------------------------------
    // static data core files are dealt with by these functions. They are
    // almost identical to the normal igc file loaders, but there is a version 
    // number in the file, and it is returned by the LoadIGCStaticCore function.
    // if the load function fails, it returns NA.
    //------------------------------------------------------------------------------
    bool    DumpIGCStaticCore (const char* name, ImissionIGC* pMission, __int64 iMaskExportTypes, void (*munge)(int size, char* data) = NULL);
    int     LoadIGCStaticCore (const char* name, ImissionIGC* pMission, bool fGetVersionOnly, void (*munge)(int size, char* data) = NULL);
    int     CacheIGCStaticCore (const char* name, ImissionIGC* pMission, bool fGetVersionOnly, void (*munge)(int size, char* data) = NULL);
    
    #endif
    And then missionigc.ccp seems to have the rest of it.

    Also at line 1036 of igc.h
    Code:
        //------------------------------------------------------------------------------
        char        strGameName[c_cbGameName];              //Name of game
        char        szIGCStaticFile[c_cbFileName];          //Name of static IGC file

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Do I need to get the variables in a certain order?
    Because I tried:
    VB Code:
    1. Type PartData
    2.       partID As Integer
    3.       amount As Integer
    4.       mountID As Byte
    5. End Type
    6.  
    7. Dim PartData As PartData, strPath As String
    8. strPath = App.Path & "\dn_000450.igc"
    9. Open App.Path & "dn_000450.igc" For Binary Access Read As #1
    10. Get #1, , PartData.mountID
    11. Close #1
    12. Debug.Print PartData.mountID
    And it’s not working. What am I doing wrong?

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Yuo have to know how the file was written or how the game loads it, your assuming that the first bytes will correspond to the PartData data structure.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by leinad31
    Yuo have to know how the file was written or how the game loads it
    What do I look for inorder to find that out And how do I make sence of it?

  18. #18
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Found it.

    int LoadIGCStaticCore (const char* name, ImissionIGC* pMission, bool fGetVersionOnly, void (*munge)(int size, char* data))

    http://svn.alleg.net:8080/svn/Allegi...missionigc.cpp

    called proc LoadIGCFile just seems to call SetConstants and move the stream pointer to skip objects. Not much help downstream... can't figure out how *munge is used since its not in icg.h.

    Maybe upstream... where is LoadICGStaticCore called?
    Last edited by leinad31; Oct 21st, 2006 at 12:19 PM.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by leinad31
    Found it.

    int LoadIGCStaticCore (const char* name, ImissionIGC* pMission, bool fGetVersionOnly, void (*munge)(int size, char* data))

    http://svn.alleg.net:8080/svn/Allegi...missionigc.cpp
    can't figure out how *munge is used since its not in icg.h.

    In that same file, there is a notation at line 61 that might clear things up ,but I'm not quite shure what it dose.
    Code:
    static void DoDecrypt(int size, char* pdata)
    {
        DWORD encrypt = 0;
        //Do a rolling XOR to demunge the data
        for (int i = 0; (i < size); i += 4)
        {
            DWORD*  p = (DWORD*)(pdata + i);
    
            encrypt = *p = *p ^ encrypt;
        }
    }

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Just out of curiosity, what is the C++ equivalent of Get in VB?


    Also, Coredump has been updated:
    http://www.bitbucket.ca/~acheater/alleg/

    And I also forgot to mention that you need igc.py for coredump.py to work.



    Also, dose anyone know how to convert Python to Visual Basic 6.0?
    Even though I would very much like to use Get to retrieve the data from the file, I’m not having much luck finding the read order of the variables in the C++ game source code and I may have to use the method that CoreDump uses. – My best guess from looking at the Coredump and igc.py Python source code is that it reads the individual bytes instead of retrieving the types.
    (Note: I’m still guessing on most of this stuff since I don’t know C++ or Python.)
    Last edited by Tontow; Oct 22nd, 2006 at 08:12 PM. Reason: added another question and more information

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    I did a search for the word munge in the game's source code using Visual C++ 2005 Express Edition and came up with several hits of "ScrambleMunge" in \zlib\zstring.cpp at lines 911, 978, 983, 988 and of "ScrambleUnmunge" in \zlib\zstring.cpp at lines 937, 1006, 1011, 1016 and of "XMunge" in \xmunge\xmunge.cpp at lines 60, 45 .
    Dose anyone have any further help or input?




    In the mean time, I've managed to make a function to read the .csv output of CoreDump since I have not figured out how to read the variables from the .igc file. If anyone has any suggestions on how I can make it better, then it would be greatly welcome.

    VB Code:
    1. Private Sub Form_Load()
    2. Dim testdata() As String
    3. testdata() = getdata("launchers", "expendable_id")
    4. MsgBox (testdata(UBound(testdata())))
    5. End Sub
    6.  
    7. Public Function getdata(file As String, var As String) As String()
    8. ''''''''''
    9. Dim sfile As String
    10. sfile = App.Path & "\dn_000450\" & file & ".csv"
    11. Dim myArray() As String
    12. ReDim myArray(0)
    13. Open sfile For Input As #1
    14. Do Until EOF(1)
    15.    Input #1, myArray(UBound(myArray))
    16.    ReDim Preserve myArray(UBound(myArray) + 1)
    17. Loop
    18. ReDim Preserve myArray(UBound(myArray) - 1)
    19. Close #1
    20. ''''''''''''''
    21. 'get the number of varable names (use numofvarname for step)
    22. Dim numofvarname As Integer
    23. numofvarname = 0
    24. Do Until myArray(numofvarname) = ""
    25. numofvarname = numofvarname + 1
    26. Loop
    27. 'MsgBox ("number of varable names: " & numofvarname & " : " & myArray(numofvarname))
    28. 'match the varble names
    29. Dim varnameindex
    30. For i = LBound(myArray()) To UBound(myArray())
    31. If myArray(i) = var Then varnameindex = i: Exit For
    32. If i > numofvarname Then varnameindex = -1: MsgBox ("ERROR:" & vbCrLf & "Varable: " & var & " Not found in: " & file): Exit For
    33. Next i
    34. 'MsgBox ("Varable name index: " & varnameindex & vbCrLf & "Reads: " & myArray(varnameindex))
    35. 'gather varable array (text seems to come out correct)
    36. Dim index As Integer
    37. index = 0
    38. Dim vararray() As String
    39. ReDim vararray(0)
    40. For i = (LBound(myArray()) + numofvarname - 1) To UBound(myArray()) Step (numofvarname + 1)
    41. ReDim Preserve vararray(UBound(vararray) + 1)
    42. If (i + varnameindex + 2) <= UBound(myArray()) Then vararray(index) = myArray(i + varnameindex + 2)
    43. index = index + 1
    44. Next i
    45. ReDim Preserve vararray(UBound(vararray) - 2)
    46.  
    47. getdata = vararray()
    48.  
    49. End Function

  22. #22
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Yes, might as well use the CSV rather than reinventing the wheel ^^

    Post the CSV so we have something to work with

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by leinad31
    Yes, might as well use the CSV rather than reinventing the wheel ^^

    Post the CSV so we have something to work with
    But I want to reinvent the wheel, just think of what one could make off the copyright.

    BTW, Usage of the function is getdata("name of the file here", "variable that you want to get") It should return with an array of values based on what is in the fist row.
    Here’s the zip.
    Last edited by Tontow; Oct 24th, 2006 at 09:48 PM.

  24. #24
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    You can use ADO and OLEDB.

    GetData() is similar to your getdata in terms of parameters and return value (string array) only it uses ADO.

    GetDataEx() returns a recordset

    Form_Load() tests them

    VB Code:
    1. Option Explicit
    2.  
    3. ' filenames less extension. also worksheet names
    4. Private Const FNM_BOOSTERS = "boosters#csv"
    5. Private Const FNM_CHAFF = "chaff#csv"
    6. Private Const FNM_CHAFF_TABLE = "chaff_table#csv"
    7. Private Const FNM_CLOAKS = "cloaks#csv"
    8. Private Const FNM_DMGTABLE = "dmgtable#csv"
    9. Private Const FNM_DRONES = "drones#csv"
    10. Private Const FNM_FACTIONS = "factions#csv"
    11. Private Const FNM_MINES = "mines#csv"
    12. Private Const FNM_MISSILES = "missiles#csv"
    13. Private Const FNM_PARTS = "parts#csv"
    14. Private Const FNM_PROBES = "probes#csv"
    15. Private Const FNM_PROJECTILES = "projectiles#csv"
    16. Private Const FNM_SHIELDS = "shields#csv"
    17. Private Const FNM_SHIPS = "ships#csv"
    18. Private Const FNM_STATIONS = "stations#csv"
    19. Private Const FNM_TECH = "tech#csv"
    20. Private Const FNM_TREASURE = "treasure#csv"
    21. Private Const FNM_WEAPONS = "weapons#csv"
    22.  
    23.  
    24. Private cn As New ADODB.Connection  'better if public in a module
    25.  
    26.  
    27. Private Sub Form_Load()
    28. Dim sTmp As String
    29.  
    30.    sTmp = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    31.       App.Path & "\dn_000450\;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
    32.    cn.Open sTmp
    33.  
    34.    Debug.Print "FNM_MISSILES" & vbCrLf & Join(GetData(FNM_MISSILES, "abilities"), vbCrLf) & vbCrLf
    35.    Debug.Print "FNM_BOOSTERS" & vbCrLf & Join(GetData(FNM_BOOSTERS, "description"), vbCrLf) & vbCrLf
    36.    
    37. Dim rsRet As ADODB.Recordset
    38.    
    39.    Debug.Print GetDataEx(rsRet, FNM_MINES, "ld_description")
    40.    If Not (rsRet Is Nothing) Then
    41.       Debug.Print rsRet.GetString(, , vbTab, vbCrLf, "") & vbCrLf
    42.       rsRet.Close
    43.       Set rsRet = Nothing
    44.    End If
    45. End Sub
    46.  
    47.  
    48. Private Sub Form_Unload()
    49.    If cn.State <> adStateClosed Then cn.Close
    50.    Set cn = Nothing
    51. End Sub
    52.  
    53.  
    54. Public Function GetData(ByVal tblname As String, ByVal colname As String) As String()
    55. Dim rs As ADODB.Recordset
    56. Dim sTmp As String
    57. Dim bOk As Boolean
    58.    
    59.    If cn.State = adStateClosed Then Exit Function
    60.    
    61.    'check if tblname+colname exists
    62.    bOk = True 'init retval
    63.    Set rs = cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, tblname, colname))
    64.    If rs Is Nothing Then
    65.       bOk = False 'error, unable to return recordset
    66.    ElseIf rs.BOF And rs.EOF Then
    67.       bOk = False 'error, tblname+colname does not exist
    68.       rs.Close
    69.    Else
    70.       rs.Close
    71.    End If
    72.      
    73.    'query
    74.    If bOk Then
    75.       rs.CursorLocation = adUseClient
    76.       rs.Open "SELECT " & colname & " FROM " & tblname, cn, adOpenStatic
    77.       If rs.BOF And rs.EOF Then  'no records
    78.          rs.Close
    79.       Else
    80.          sTmp = rs.GetString(, , vbTab, vbCrLf, "")
    81.          GetData = Split(sTmp, vbCrLf)  'since your original implementation returned strings
    82.          rs.Close
    83.       End If
    84.    End If
    85.    Set rs = Nothing
    86. End Function
    87.  
    88.  
    89. Public Function GetDataEx(ByRef rsRet As ADODB.Recordset, ByVal tblname As String, ByVal colname As String) As Long
    90. Dim sTmp As String
    91. Dim bOk As Boolean
    92.  
    93.    GetDataEx = -1 'init retval failed
    94.    If cn.State = adStateClosed Then Exit Function
    95.    
    96.    'check if tblname+colname exists
    97.    bOk = True 'init retval
    98.    Set rsRet = cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, tblname, colname))
    99.    If rsRet Is Nothing Then
    100.       bOk = False 'error, unable to return recordset
    101.    ElseIf rsRet.BOF And rsRet.EOF Then
    102.       bOk = False 'error, tblname+colname does not exist
    103.       rsRet.Close
    104.    Else
    105.       rsRet.Close
    106.    End If
    107.      
    108.    'query
    109.    If bOk Then
    110.       rsRet.CursorLocation = adUseClient
    111.       rsRet.Open "SELECT " & colname & " FROM " & tblname, cn, adOpenStatic
    112.       GetDataEx = 0  'retval success
    113.    End If
    114. End Function

    Actually you could place in a separate procedure the tblname+colname check and have the procedure return a boolean.
    Last edited by leinad31; Oct 24th, 2006 at 11:56 PM.

  25. #25
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by Tontow
    But I want to reinvent the wheel, just think of what one could make off the copyright.

    BTW, Usage of the function is getdata("name of the file here", "variable that you want to get") It should return with an array of values based on what is in the fist row.
    Here’s the zip.
    Yes, but then you'd be forced to implement the original data structure, we'd also have to build class modules for the classes in C++.

    While if we manipulate the CSV, we can treat the CSV's as tables through ADO+SQL which is easier especially if your looking for particular records or returning particular columns. So development moves on instead of being stuck in translation phase.

    Later on we can go back and implement an on demand creation of the CSV's ... If I'm not mistaken, and if the dll function's parameter data types are supported in VB, we can reference the CoreDump function in the C++ dll like what we do with API calls. So we declare the dll function, call it in VB then access the CSVs it creates.
    Last edited by leinad31; Oct 25th, 2006 at 12:05 AM.

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    I'm getting an error with your code: "user defined type not defined"

    How do I fix it?/what is the correct way to setup your code?

  27. #27
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    It ran fine here... on what line was the error? And what were the values of the passed parameters?

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    VB Code:
    1. Private cn As New ADODB.Connection  'better if public in a module

    Did you add any references or components? What where there names?

  29. #29
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    That needs a reference to Microsoft ActiveX Data Objects.. pick a version I used 2.8

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    I tried timing the speed of the code. It shows my code to be way faster than yours.

    Did I do the timing it right?

    BTW, Thankyou everyone for all your help thus far.

    (NOTE: I have 3 text boxes on my form txt1 txt2 and txt3, my results show 6 sec for txt1, 5 sec for txt2 and 1 sec for txt3 . txt1 and txt2 show your methods and txt3 shows mine.)
    VB Code:
    1. Option Explicit
    2.  
    3. ' filenames less extension. also worksheet names
    4. Private Const FNM_BOOSTERS = "boosters#csv"
    5. Private Const FNM_CHAFF = "chaff#csv"
    6. Private Const FNM_CHAFF_TABLE = "chaff_table#csv"
    7. Private Const FNM_CLOAKS = "cloaks#csv"
    8. Private Const FNM_DMGTABLE = "dmgtable#csv"
    9. Private Const FNM_DRONES = "drones#csv"
    10. Private Const FNM_FACTIONS = "factions#csv"
    11. Private Const FNM_MINES = "mines#csv"
    12. Private Const FNM_MISSILES = "missiles#csv"
    13. Private Const FNM_PARTS = "parts#csv"
    14. Private Const FNM_PROBES = "probes#csv"
    15. Private Const FNM_PROJECTILES = "projectiles#csv"
    16. Private Const FNM_SHIELDS = "shields#csv"
    17. Private Const FNM_SHIPS = "ships#csv"
    18. Private Const FNM_STATIONS = "stations#csv"
    19. Private Const FNM_TECH = "tech#csv"
    20. Private Const FNM_TREASURE = "treasure#csv"
    21. Private Const FNM_WEAPONS = "weapons#csv"
    22.  
    23.  
    24. Private cn As New ADODB.Connection  'better if public in a module
    25.  
    26.  
    27. Private Sub form_Load()
    28.     Dim T1 As Variant
    29.     Dim T2 As Variant
    30.  
    31. 'method 1
    32. T1 = Now
    33.  
    34. Dim sTmp As String
    35.    sTmp = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    36.       App.Path & "\dn_000450\;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
    37.    cn.Open sTmp
    38.    
    39.    txt1.Text = Join(getdata(FNM_TECH, "name"), vbCrLf)
    40.    'Debug.Print "FNM_BOOSTERS" & vbCrLf & Join(GetData(FNM_BOOSTERS, "description"), vbCrLf) & vbCrLf
    41.  
    42. T2 = Now
    43. txt1.Text = txt1.Text & vbCrLf & Format(DateDiff("s", T1, T2), "0") & " s"
    44.    
    45. 'method 2
    46. T1 = Now
    47.  
    48. Dim rsRet As ADODB.Recordset
    49.    
    50.    Call GetDataEx(rsRet, FNM_TECH, "name")
    51.    If Not (rsRet Is Nothing) Then
    52.       txt2.Text = rsRet.GetString(, , vbTab, vbCrLf, "")
    53.       rsRet.Close
    54.       Set rsRet = Nothing
    55.    End If
    56.  
    57. T2 = Now
    58. txt2.Text = txt2.Text & vbCrLf & Format(DateDiff("s", T1, T2), "0") & " s"
    59.    
    60. 'method 3
    61. T1 = Now
    62.  
    63.    txt3.Text = Join(ggetdata("tech", "name"), vbCrLf)
    64.    
    65.  T2 = Now
    66. txt3.Text = txt3.Text & vbCrLf & Format(DateDiff("s", T1, T2), "0") & " s"
    67.  
    68.    
    69. End Sub
    70.  
    71.  
    72. Private Sub frm1_Unload()
    73.    If cn.State <> adStateClosed Then cn.Close
    74.    Set cn = Nothing
    75.    MsgBox ("close")
    76. End Sub
    77.  
    78.  
    79. Public Function getdata(ByVal tblname As String, ByVal colname As String) As String()
    80. Dim rs As ADODB.Recordset
    81. Dim sTmp As String
    82. Dim bOk As Boolean
    83.    
    84.    If cn.State = adStateClosed Then Exit Function
    85.    
    86.    'check if tblname+colname exists
    87.    bOk = True 'init retval
    88.    Set rs = cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, tblname, colname))
    89.    If rs Is Nothing Then
    90.       bOk = False 'error, unable to return recordset
    91.    ElseIf rs.BOF And rs.EOF Then
    92.       bOk = False 'error, tblname+colname does not exist
    93.       rs.Close
    94.    Else
    95.       rs.Close
    96.    End If
    97.      
    98.    'query
    99.    If bOk Then
    100.       rs.CursorLocation = adUseClient
    101.       rs.Open "SELECT " & colname & " FROM " & tblname, cn, adOpenStatic
    102.       If rs.BOF And rs.EOF Then  'no records
    103.          rs.Close
    104.       Else
    105.          sTmp = rs.GetString(, , vbTab, vbCrLf, "")
    106.          getdata = Split(sTmp, vbCrLf)  'since your original implementation returned strings
    107.          rs.Close
    108.       End If
    109.    End If
    110.    Set rs = Nothing
    111. End Function
    112.  
    113.  
    114. Public Function GetDataEx(ByRef rsRet As ADODB.Recordset, ByVal tblname As String, ByVal colname As String) As Long
    115. Dim sTmp As String
    116. Dim bOk As Boolean
    117.  
    118.    GetDataEx = -1 'init retval failed
    119.    If cn.State = adStateClosed Then Exit Function
    120.    
    121.    'check if tblname+colname exists
    122.    bOk = True 'init retval
    123.    Set rsRet = cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, tblname, colname))
    124.    If rsRet Is Nothing Then
    125.       bOk = False 'error, unable to return recordset
    126.    ElseIf rsRet.BOF And rsRet.EOF Then
    127.       bOk = False 'error, tblname+colname does not exist
    128.       rsRet.Close
    129.    Else
    130.       rsRet.Close
    131.    End If
    132.      
    133.    'query
    134.    If bOk Then
    135.       rsRet.CursorLocation = adUseClient
    136.       rsRet.Open "SELECT " & colname & " FROM " & tblname, cn, adOpenStatic
    137.       GetDataEx = 0  'retval success
    138.    End If
    139. End Function
    140.  
    141. Public Function ggetdata(file As String, var As String) As String()
    142. ''''''''''
    143. Dim sfile As String
    144. sfile = App.Path & "\dn_000450\" & file & ".csv"
    145. Dim myArray() As String
    146. ReDim myArray(0)
    147. Open sfile For Input As #1
    148. Do Until EOF(1)
    149.    Input #1, myArray(UBound(myArray))
    150.    ReDim Preserve myArray(UBound(myArray) + 1)
    151. Loop
    152. ReDim Preserve myArray(UBound(myArray) - 1)
    153. Close #1
    154. ''''''''''''''
    155. 'get the number of varable names (use numofvarname for step)
    156. Dim numofvarname As Integer
    157. numofvarname = 0
    158. Do Until myArray(numofvarname) = ""
    159. numofvarname = numofvarname + 1
    160. Loop
    161. 'MsgBox ("number of varable names: " & numofvarname & " : " & myArray(numofvarname))
    162. 'match the varble names
    163. Dim varnameindex
    164. Dim i As Integer
    165. For i = LBound(myArray()) To UBound(myArray())
    166. If myArray(i) = var Then varnameindex = i: Exit For
    167. If i > numofvarname Then varnameindex = -1: MsgBox ("ERROR:" & vbCrLf & "Varable: " & var & " Not found in: " & file): Exit For
    168. Next i
    169. 'MsgBox ("Varable name index: " & varnameindex & vbCrLf & "Reads: " & myArray(varnameindex))
    170. 'gather varable array (text seems to come out correct)
    171. Dim index As Integer
    172. index = 0
    173. Dim vararray() As String
    174. ReDim vararray(0)
    175. For i = (LBound(myArray()) + numofvarname - 1) To UBound(myArray()) Step (numofvarname + 1)
    176. ReDim Preserve vararray(UBound(vararray) + 1)
    177. If (i + varnameindex + 2) <= UBound(myArray()) Then vararray(index) = myArray(i + varnameindex + 2)
    178. index = index + 1
    179. Next i
    180. ReDim Preserve vararray(UBound(vararray) - 2)
    181.  
    182. ggetdata = vararray()
    183.  
    184. End Function

  31. #31
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need help reading a data file. (Visual Basic 6.0)

    Of course arrays are faster than initializing a recordset... but then you would have a hard time getting relationships out of all those tables... but if there are no relationships or they are not normalized tables then go ahead with the by array extraction.

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: I need help reading a data file. (Visual Basic 6.0)

    Quote Originally Posted by leinad31
    but then you would have a hard time getting relationships out of all those tables... but if there are no relationships or they are not normalized tables then go ahead with the by array extraction.
    What do you mean by relationships and normalized tables?

    If your talking about formulas that involve more than one table, then there are none. And there all *.csv (Comma Separated Values Files) if that’s what you meant by normalized tables.

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