Results 1 to 6 of 6

Thread: Read an UDT data file in VC++

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    I know how to do this in VB. But can anyone teach me how to archieve this in VC++?

    Code:
    Option Explicit
    Private Type sData
        ID As String
        Age As Integer
        Name As String
    End If
    Private sd As sData
    
    Private Sub ReadFile()
    Dim Ptr As Long
    
        Open "C:\1.dat" For Binary As #1
            Ptr = 0
            Do While Not EOF(1)
                Get #1, (Ptr * Len(sd)) + 1, sd
                Debug.Print sd.ID
                Debug.Print sd.Age
                Debug.Print sd.Name
                
                Ptr = Ptr + 1
                DoEvents
            Loop
        Close #1
    End Sub

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Something like this:
    Code:
    #include <stdio.h>
    
    struct MyType {
        int iNum;
        bool bX;
    };
    
    void main() {
        FILE *pFile = fopen("temp.dat", "wb");
        MyType X = {5, true};
        MyType Y;
    
        fwrite(pFile, &X, sizeof(MyType);
        fclose(pFile);
    
        pFile = fopen("temp.dat", "rb");
        fread(pFile, &Y, sizeof(MyType);
    
        fclose(pFile);
    
        printf("%d = %d", iNum, bX);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Parksie, can it be done by using the ReadFile API function? and use pointer to read various section of the file?

  4. #4

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Parksie, When I run your code, I get some error and later on I modified the here and there and it work now.

    Code:
    '//Code in MyProject.h
    void ReadWrite(HWND);
    
    typedef struct {
    	int iNum;
    	bool bX;
    }MyType;
    MyType X;
    MyType Y;
    Code:
    '//Code in MyProject.cpp
    void ReadWrite(HWND hDlg)
    {
        FILE *pFile = fopen("c:\\1.dat", "wb");
        X.bX = true;
    	X.iNum = 5;
    
        fwrite(&X, sizeof(MyType), sizeof(MyType), pFile);
        fclose(pFile);
    
        pFile = fopen("C:\\1.dat", "rb");
        fread(&Y, sizeof(MyType), sizeof(MyType), pFile);
    
        fclose(pFile);
    
        printf("%d = %d", Y.iNum, Y.bX);
    	return;
    }

    Do you have any idea on how to read the file by a given address location?

  5. #5

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Parksie, I found that I can use the SetFilePointer API to move the open file pointer to the location I need. But Do you know how to use the OpenFile API to open a Binary file?

    Thx

  6. #6

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    can someone explain this to me?

    Code:
    typedef struct {
        long ID;
        float fees;
        double pt;
    } TEST;
    TEST ut;
    
    void ReadWrite(HWND hDlg)
    {
        FILE *pFile;
        //Open the map data file
        pFile = fopen("C:\\2.dat", "rb");
    
        //Read the Genral Information abt the Map data file
        fread(&ut, sizeof(ut), 1, pFile);
    
        //Close the Map data file
        fclose(pFile);
    
        return;
    }
    if I include the extra element call Name then I will not be able to read the data correctly from a binary file as I read with the above method.

    Code:
    typedef struct {
        char Name[5];
        long ID;
        float fees;
        double pt;
    } TEST;
    TEST ut;
    
    void ReadWrite(HWND hDlg)
    {
        FILE *pFile;
        //Open the map data file
        pFile = fopen("C:\\2.dat", "rb");
    
        //Read the Genral Information abt the Map data file
        fread(&ut, sizeof(ut), 1, pFile);
    
        //Close the Map data file
        fclose(pFile);
    
        return;
    }
    But If i resort the structure as below, I'll be able to read the data correctly from a binary file

    Code:
    typedef struct {
        long ID;
        float fees;
        double pt;
        char Name[5];
    } TEST;
    TEST ut;
    
    void ReadWrite(HWND hDlg)
    {
        FILE *pFile;
        //Open the map data file
        pFile = fopen("C:\\2.dat", "rb");
    
        //Read the Genral Information abt the Map data file
        fread(&ut, sizeof(ut), 1, pFile);
    
        //Close the Map data file
        fclose(pFile);
    
        return;
    }

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