Binary data reading

This is a discussion on Binary data reading within the C++ Programming forums, part of the General Programming Boards category; Hi, all. I'm having some trouble with data being written in binary file i/o. Here's the gist of what I'm ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Binary data reading

    Hi, all. I'm having some trouble with data being written in binary file i/o. Here's the gist of what I'm doing:
    I have an array (MyMap[3]) of a struct that has one int and one long. I want to record the values of the structs into a file.
    Simple enough, right? Except that the last set of data in my array records "1" for the int data, yet records the proper value of the long.

    Here's the relevant code:

    Code:
    //declared at the start of the file (after the headers, etc.)
    struct Map {
    	int MyInt;
    	long MyLong;
    };
    
    Map MyMap[3];
    int iMapCntr=0
    
    //triggered when the left mouse button is clicked:
    //Writes struct array data to file sequentially(0 to 3)
    ULONG file_ptr;
    file_ptr=0;
    HANDLE hFile=CreateFile("test.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    for(iMapCntr=0;iMapCntr<4;iMapCntr++)
    {
        file_ptr=iMapCntr*sizeof(Map);
       WriteFile(hFile,&MyMap[iMapCntr],sizeof(Map),&file_ptr,NULL);
    }
    CloseHandle(hFile);
    
    //triggered when the right mouse button is clicked:
    //Reads struct array data in from file sequentially(0 to 3)
    ULONG file_ptr;
    file_ptr=0;
    HANDLE hFile=CreateFile("test.dat",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    for(iMapCntr=0;iMapCntr<4;iMapCntr++)
    {
         file_ptr=iMapCntr*sizeof(Map);
         ReadFile(hFile,&MyMap[iMapCntr],sizeof(Map),&file_ptr,NULL);
         
         //for debugging puposes
         char cInt[20], cLong[20], cSep[5]=" , ";
         _itoa(MyMap[iMapCntr].MyInt,cInt,10);
         _itoa(MyMap[iMapCntr].MyLong,cLong,10);
        strcat(cInt,cSep);
        strcat(cInt,cLong);
        MessageBox(hWndMain,cInt,"Test",NULL);
       //end debugging code
    
    }
    CloseHandle(hFile);
    Everything returns properly except on the last call MyMap[3].MyInt returns 1.
    The values are set with keystrokes not listed above. I've even tried hardcoding the MyInt value and it still returns 1.
    Am I incorrectly setting the file_ptr variable?
    Thanks in advance for any help you can give.
    Jason

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Map MyMap[3];

    This array only has three elements.
    Change this to:

    Map MyMap[4];

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    array elements

    I thought the elements an array were like those in VB - - which is what I'm migrating from(I should have known better... ), in other words:

    myarray[3] declares an array of 4 elements, 0, 1, 2 and 3.

    Do you mean that in C/C++, myarray[3] declares an array of 3 elements, 0, 1 and 2?

    Thanks for the response.
    Jason

  4. #4
    Unregistered
    Guest
    yep

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Reading and writing binary data
    By Yasir_Malik in forum C Programming
    Replies: 3
    Last Post: 12-12-2004, 09:24 AM
  2. Reading a line of data...
    By RedZippo in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2004, 03:14 AM
  3. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21