Visual C++ Reading Binary Files

First of all, I am a C programmer but am trying to write an application in Visual C++ in order to have the nice GUI. In my program I have to read the data in a binary file and store it in a data structure. This is easy in C using the fread() routine but in Visual C++ I cannot figure out how to do this and can't find any good examples doing a google search that aren't super complicated and don't give me compile errors.

My input file is stored in my text box.

txtRecordFile->Text

I want to open this file in binary mode and read the file until end of file and store each read into a structure that looks like...

struct Format11
{
unsigned id : 17;
unsigned notUsed : 1;
unsigned NotInLock : 1;
unsigned Parity : 1;
unsigned minor_time : 12;
};

struct Format11 myData;
Here's an example of reading from a binary file which contains numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>

int main()
{
    std::ifstream binaryFile("binaryFile.txt", std::ios::binary);
    unsigned firstValue, secondValue, thirdValue;
    
    binaryFile >> firstValue;
    binaryFile >> secondValue;
    binaryFile >> thirdValue;
    
    binaryFile.close();
    return 0;
}


If that isn't what you wanted, let me know. I hope this helps.

Edit: This assumes each value in the binary file is separated by one newline.
Edit: Apologies, I did misunderstand.
Last edited on
LASims wrote:
This is easy in C using the fread() routine
You can do that in Visual C++ too. You should be able to use your C code in a C++ environment without problems.

@Wyboth
This would only work if "binaryFile.txt" contains text and not indeed binary like the struct Format11 of the OP implies

First of all, I am a C programmer but am trying to write an application in Visual C++ in order to have the nice GUI.


You can 'have the nice GUI' with C programs too. To turn a C coder into an instant C++ coder just compile with C++ instead of C, i.e., change your file extensions from *.c to *.cpp. It worked for me 15 years ago!!!! :)

All kidding aside, I'm pretty much with coder777 on this. If you have code working in C, just use it as is. The C Standard Lib is part of C++.

That leaves the GUI issue. Do you know how to create a GUI in C?
Last edited on
Thanks for the replies. So it appears that the best method may be to use the Visual C++ GUI code mixed with the C code for file manipulation. I run into some problems when the two worlds butt heads in my program.

My filename is stored in a textbox

txtRecordFile->Text

but I cannot use that in my fopen() statement because it is not of the correct type. How to use the txtRecordFile->Text in the fopen() statement?

Thanks in advance.
LASims
Although you haven't clearly stated it LASims, I suspect you are using Visual Studio and the CLI ???? If so, you should clearly state that here because that application framework is very much different from the way C or C++ coders would deal with graphical objects.

That really is a different animal from what C or C++ coders think of as Windows Programming. From what I gather this is a forum that handles CLI programming, although I have to admit I never did any of that. However, I am aware that in that programming paradigm something like a text box (C and C++ programmers call them edit controls) is accessed like so...

txtRecordFile->Text

Such an access is going to return a string of characters in some form. That's what you need to find out. I'm not sure in what form CLI programs return character data. Likely through the String Class contained within the .NET framework. Other possibilities would be the C++ Standard Library String Class, but I'm 100% certain a CLI program wouldn't return a String in that form. So do you see why it is necessary to determine what form such a string access as this...

txtRecordFile->Text

returns its data in?

For as you should understand if you are indeed a C programmer, the C Standard Library functions that take character strings as parameters expect strings to be NULL terminated strings, and they expect a pointer to the first character of the string. For example, if you have this...

1
2
3
FILE* fp=NULL;
char szFileName[]="C:\\Tallies\\Data\\MyData.dat";
fp=fopen(szFileName,"r");


...then szFileName is a pointer to the first byte of the character string containing the file path. That is what you need to obtain from your CLI framework, .NET, or whatever libraries are being used by whatever you are doing. From whatever form that string is returned to you in txtRecordFile->Text, you are going to have to manipulate that String so as to obtain the pointer to the first byte of the actual data in memory. In C++'s String class, one would use its String::c_str() member function to return a C pointer to the first byte of the String controlled by the String Class.

Hope this helps.
Last edited on
Freddie, thanks for the thoughtful reply. I am using Microsoft C++ .net 2003 version. If I hover the mouse on the txtRecordFile->Text is says it is of type

String __gc

I need to somehow convert the text in this textbox into my C variable

char inputFile[255];

You would think this would be easy to do. You have a string of characters in a textbox, one ascii character after the other and you want to put it into an array of characters. But this programming environment makes simple stuff a massive headache. Thoughts?

vr, LASims
So you are using CLI crap. Why do you do that if you don't understand the language ?

It is NOT C++ and NOT C, is a Microsoft proprietary language and even Microsoft has abandoned it in later versions of Visual Studio.
Last edited on
Alas, modoran is not being so diplomatic as I; but the case isn't lost if that framework can return that pointer to the actual data so it can be used in the C Runtime functions.

But I would have questions as to whether or not it can. Wasn't the whole purpose of all that .NET stuff to prevent not so able application programmers from shooting themselves in the foot with dangerous weapons such as memory pointers? If so, then that pointer can't be returned and you are likely out of luck.

When I first saw your post I (and I think others too) thought the best way foreward for you would be to learn to code GUI prpograms using the Windows Api directly in C. Being as the Windows Api was likely written in C, and being as it is definitely documented in C, its kind of logical for C and C++ coders to use C to create GUIs. Your C code would then fit right in with no difficulties. My own code is largely a mixture of what I'd refer to as C'isms and C++'isms. I use C++ classes and C++ isms where it suits me and C for the rest.

But if you wish to persist with the CLI, .NET, C++.NET, or whatever its now called, then your best way forward is to likely learn that framework, and redo your C code within that framework. I think a lot of us would think it better to abandon the CLI, .NET stuff, but that's a personal decision you'll have to make.
Ok, thanks. I am just using what I have been given at the office. If I wanted to use and learn the Windows Api in C/C++ what compiler package would I have my company buy me? Or is there something downloadable for free?

Thanks for reading.

vr, LASims
I guess in the back of my mind I'm trying to nudge you in the direction of doing standard C or C++ 'unmanaged' code, but here is an example of how easy it is to get a null terminated character string out of a textbox when using the Windows Api directly in C or C++. The following code is exactly the same whether the code is compiled as C or C++, and hParent in the following code is something you would have (its a HANDLE to a top level window), and you would have IDC_TEXTBOX too - its just a numeric identifier you would have assigned yourself)...

1
2
3
4
5
6
7

HWND hTextBox=NULL;    // Window Handle of TextBox
char szBuffer[512];           // buffer to store retrieved text

hTextBox=GetDlgItem(hParent, IDC_TEXTBOX);
GetWindowText(hTextBox,szBuffer,512);


After the above code runs tou would have your null terminated file path in the szBuffer buffer, and that coud be used directly in an fopen() call, as it is a pointer to the file path retrieved out of the text box.

That's really not that hard.

If you like (just ask) I can put together a small sample C program for you that puts up a window with a text box on it and a button, and when you click the button it writes some data to a text file whose path you would have typed in the text box. That would get you started.
Last edited on
Yes Freddie, I'd like the example program if you have the time. Might also need some instruction on how to setup the new project in my or some other compiler.

Thanks, LASims
You can still use Visual Studio 2003 just fine. I sometimes use Visual Studio 6 circa 1998!

First I want to make sure you can build a simple "Hello, World!" type C GUI project though. At present I don't have VS 2003 on any of my boxes; just VC 6 Enterprise Edition and VS 2008 Pro. Anyway, here is a test program as described above that should create a window but without the "Hello, World!". Just save the file as Main.c after you follow my directions as described below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    Main.c

    To run this code you should tell Visual Studio to set up a GUI Project.  In the
    options presented to you through the various visual interfaces of Visual Studio
    you should tell it to set up an 'Empty Project'.  If there are options to specify
    C or C++ compilation, you can choose C compilation if you like.  An 'Empty Project'
    is one where Visual Studio won't auto-generate any wizard code for you.  After
    Visual Studio is done creating your 'Empty Project', you'll need to tell it to
    create a new blank *.c file for you and to add it to the project.  The menu
    commands for doing that should be under the File menu somewhere.

    After doing that paste this code into the new blank Main.c file and attempt to
    compile.  The only possibility of failure that I can see with Visual Studio 2003
    is that I don't know when Microsoft set up its Visual Studio Environment to
    default to wide character (UNICODE) builds.  It wasn't so set in Visual Studio 6
    circa 1998 or so, but it was so set around 2008 with Visual Studio 2008 (VC9). I
    don't presently have a 2003 setup to test this on.

    If you do receive any errors about character strings that's what's happening.  In
    that case you need to go into 'Project Settings' and specify the 'multi-byte
    character set or leave the character set as 'Not Set'.  I purposly used chars in
    this code instead of the wchar_ts I usually use because I figurred you would be
    more at home with them.  I'd guess most folks compile with UNICODE nowadays, as do
    I.
*/

#include <windows.h>

LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 if(msg==WM_DESTROY)       // This is the Window Procedure.  The concept of the
 {                         // Window Procedure is the most important concept to
    PostQuitMessage(0);    // grasp in C/C++ WinApi coding.  You never call this
    return 0;              // function with your code; rather, Windows calls it
 }                         // to inform code here of events occurring.  The events
                           // are reported here as messages.
 return (DefWindowProc(hwnd, msg, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 char szClassName[]="Form1";      // This will be both the name of the app and the name of
 WNDCLASSEX wc;                   // the main window's 'Window Class'.  The concept of the
 MSG messages;                    // Window Class and its associated Window Procedure are
 HWND hWnd;                       // the most important concepts in Windows Programming.

 memset(&wc,0,sizeof(wc));                     // zero out WNDCLASSEX wc
 wc.lpszClassName = szClassName;               // Feed "Form1" into WNDCLASSEX::lpszClassName
 wc.lpfnWndProc   = fnWndProc;                 // Feed pointer ( function address ) to Window Procedure To wc
 wc.cbSize        = sizeof(WNDCLASSEX);        // Set Size
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;   // Set Background HBRUSH
 wc.hInstance     = hInstance;                 // Set HANDLE To Instance (its a virtual process memory thing)
 RegisterClassEx(&wc);                         // Let Operating System know about "Form1" Class
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}


Is it working for you???
Topic archived. No new replies allowed.