3

My job is to translate a application from C -> C++ that have been installed on a linux distribution.so I wish the functionallity of C and linux.

I have a problem with reading binary file. It says that it reaches the EOF when it encounters a ctrl-Z character before it has reached the actual end of the file.

Precious execution in bash

zcat file.txt.gz | txtToBinary | binaryToOutput

Execution in command prompt

txtToBinary.exe < file.txt | binaryToOutput.exe

Raw text file

 R  5643BYIDK           DK0016060346 11DKKXKLY 160               1
 R 10669VJK 98 1        IS0000004018  4ISKXICE 240         5000000
 M814

txtToBinary.exe - Sample Output:

^@^@^@ hello ^@    ^Z^@^@^@^@
^@^@^[SWMA ^Y^YC

The problem is that the program interprets the first ^Z as the end of file.

Tried so far

My solutions has been to do the following when compiling on windows using c++

Execution in command prompt

txtToBinary.exe < file.txt | binaryToOutput.exe
int main(int argc, char* argv []){
    int loop (args_t* args){

    for (;;){
        char data [1024];
        int temp = read_msg (data, sizeof (data));
}

int read_msg(void* data, int size){
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(0,_0_BINARY);
    if(fread(((unsigned char *)data)+sizeof(*hdr),hdr->size-sizeof (*hdr),1,stdin) != 1);
        if(feof(stdin))
             printf("End of file error\n");
}

I have also tried Cygwin which some of the answers have me. But that also failed.

StackOverflow Answers

When looking at answer here in SO, we see Windows, Windows EOF, Binary solution,Binary Mode and Stream data end at byte 26 and Reaching EOF early Windows. They tell me that:

- Windows keys (CTRL + Z, ^Z) makes an end of file

- I have to read in binary format

Community
  • 1
  • 1
eleijonmarck
  • 4,732
  • 4
  • 22
  • 24
  • 6
    Did you open the file with `fopen(filename, "rb")`? If not, do so. – Wintermute Dec 18 '14 at 11:20
  • 'Reaches end of file before it has reached the end of the file' is a contradiction in terms. – user207421 Dec 18 '14 at 11:22
  • @Wintermute Well the program takes the file as input, as seen above. Which I would like to keep. I am experimenting with opening the file like that, but that decrease the flexibility of the program – eleijonmarck Dec 18 '14 at 11:22
  • Oh, right. Does `freopen(NULL, "rb", stdin)` work? – Wintermute Dec 18 '14 at 11:31
  • @Wintermute Sadly no, Visual Studio or Windows in general will not let me use NULL for freopen. – eleijonmarck Dec 18 '14 at 11:33
  • 3
    Hum. I tried a few things, and Windows behaves strangely. After `_setmode(0, _O_BINARY);`, `fread` handles `\x1a` characters correctly, as does `read`. However, `getchar`, `getc`, and `fgetc` still give `EOF` for them, and it turns out they even do that for file streams that are opened with `"rb"`. Are you certain your problem is with the `fread` call, or do you use other read operations that could run into this sort of problem? – Wintermute Dec 18 '14 at 12:48
  • Wow, okay. Thanks, it still does not make sense for me though. Thank you for the help @Wintermute. I have extended my code so one can see what exactly happens from the beginning of the program – eleijonmarck Dec 18 '14 at 14:23

2 Answers2

3

I found the answer to my question. It had to do with where you read from. You need to put

_setmode(0,_0_BINARY);

in the main() function!!!!!!!! Remember this, otherwise other reads or writes will not be included.

eleijonmarck
  • 4,732
  • 4
  • 22
  • 24
1

fread() is part of stdio. What you're doing is opening the raw file as binary, but then doing text-mode standard I/O.

You could replace your existing fread() call with the read() system call. (That is, fread() is a library call that does some buffering, ultimately to call through to read().)

Mike Crawford
  • 2,232
  • 2
  • 18
  • 28