VecTor
VecTor

Reputation: 83

Using ReadFile() function to read Binary data in Windows

I have two programs: program A (in FORTRAN) and program B (in C++). They are connected through pipe with each other. Program B should read binary data directly from console of program A but for some reason I can not do that:

Following is the reading part of program B:

BOOL bSuccess = FALSE;
LPBYTE File_Data;
DWORD dwFileSize;
wFileSize = GetFileSize(V_hChildStd_OUT_Rd, NULL);
File_Data = new BYTE[dwFileSize+1];
bSuccess = ReadFile( V_hChildStd_OUT_Rd, File_Data, dwFileSize, &dwRead, NULL);
delete [] File_Data; 

Note: V_hChildStd_OUT_Rd is a handle to the output of program A.

If I pass one, two or three digit(s) integer number (say 1 or 10 or 100) the program works and I can get the number in File_Data array. But for higher integer numbers and all double numbers File_Data gives meaning less value. Note that for all numbers my bSuccess is TRUE! which means it can read the file. Can you please help me to solve the problem. Thanks!

Upvotes: 0

Views: 2651

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598001

You cannot use GetFileSize() with pipes, only files. To determine how much data is available for reading from a pipe, use PeekNamedPipe() instead. And pay attention to the dwRead output value, it tells you how many bytes were actually read, which can be less than how many bytes you request.

Upvotes: 2

Related Questions