troubleshooting Question

Import numbers / strings from a binary file

Avatar of Brian Withun
Brian WithunFlag for United States of America asked on
ProgrammingC++Editors IDEs
7 Comments2 Solutions663 ViewsLast Modified:
I've attached a copy of the file I'm trying to import.  I've renamed it to a .TXT so that EE will allow me to attach it, but it's not a text file.

The attached code was used to create this file, and I'd like to come up with similar code that will allow me to import the file back into the program.

I'm just not a File I/O guy.  I need some help, preferably a solution that uses FileOpen() and FileRead() to get it done.

The first 4 bytes of this file are 06 00 00 00, and they represent an integer, 6.  The second 4 bytes are 7C 02 00 00, which represent the number 636.  The remainder of the file contains 3,816 values (6x636).

The first "value" is a 1, and it's stored as a 4-byte length (01 00 00 00) and the ascii value of the digit "1", (31)

The second value is another 5 bytes .. 01 00 00 00 31 .. this is another "1"

The third value is also 5 bytes.. 01 00 00 00 32 .. this represents a "2"

..hopefully this is clear.  I'm sure you can figure out the data layout by looking at the ::ExportPicks() method.

Can someone write for me the body of this method?

void __fastcall TMyForm::ImportPicks(AnsiString sFile, TStringGrid *pGrid)
    /* help */

Thanks in advance...
Brian Withun

void __fastcall TMyForm::ExportPicks(AnsiString sFile, TStringGrid *pGrid)
    int iHandle = FileCreate(sFile);
    int iLength = 0;

    FileWrite(iHandle, (char*)&(pGrid->ColCount), sizeof(pGrid->ColCount));
    FileWrite(iHandle, (char*)&(pGrid->RowCount), sizeof(pGrid->RowCount));

    for (int y=0; y<pGrid->RowCount; y++)
        for (int x=0; x<pGrid->ColCount; x++)

        // Write out the length of each string, followed by the string itself.

        iLength = pGrid->Cells[x][y].Length();

        FileWrite(iHandle, (char*)&iLength, sizeof(iLength));
        FileWrite(iHandle, pGrid->Cells[x][y].c_str(), pGrid->Cells[x][y].Length());




Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros