0

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!

VecTor
  • 83
  • 3
  • 13

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for quick reply. I will try PeekNamedPipe(). But even if I specify the size as a constant number for example 8 bytes for a double number it still can not read the double number: File_Data = new BYTE[8] – VecTor Mar 29 '13 at 00:32
  • @VecTor Are you sure that FORTRAN and C++ are using the same format for doubles? It's kind of crucial for your program that they are. – john Mar 29 '13 at 00:36
  • If I do not use the pipe and just write to a binary file with FORTRAN and read from the file by C++ it works fine: file.read ((char*)&a,sizeof(double)); The problem is when I use ReadFile() function and pipe. – VecTor Mar 29 '13 at 00:39
  • Are you sure that the Fortran app is actually writing data to the pipe? How are you hooking up the pipe in the first place? – Remy Lebeau Mar 29 '13 at 01:09
  • @RemyLebeau I am using anonymous pipes not named pipes! I tried PeekNamedPipe() and it does not work either. – VecTor Apr 01 '13 at 15:43
  • @RemyLebeau I checked what is written on the console of FORTRAN program in the pipe process. If I make the program to write on a file the signs ( it is in binary) are exactly same as the console ones. So I think the FORTRAN is actually writing data to the pipe. – VecTor Apr 01 '13 at 15:49
  • @VecTor: [Read the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365779.aspx): "Copies data from a named **or anonymous pipe** into a buffer without removing it from the pipe. It also returns information about data in the pipe." – Remy Lebeau Apr 01 '13 at 16:17
  • @RemyLebeau But why it does not work: PeekNamedPipe(V_hChildStd_OUT_Rd[n], File_Data1, dwFileSize, &dwRead, &dwReadAvail, NULL); – VecTor Apr 01 '13 at 16:39
  • 1
    @VecTor: I cannot answer that since you have not indicated **HOW** it is failing, or shown **HOW** you are using it in the context of your code, or shown **HOW** you are initialing the pipe. Are you still using `GetFileSize()` to initialize `dwFileSize`, for instance? Don't do that. What error is `PeekNamedPipe()` actually reporting? You need to be more specific if you want people to help you. – Remy Lebeau Apr 01 '13 at 18:52