Prevent Windows program from interpreting ^ Z as end of file

My task is to translate an application from C -> C ++ that was installed in the linux distribution.so I would like the functionality of C and linux.

I have a problem reading a binary file. It says that it reaches EOF when it encounters a character ctrl-Z

, before it reaches the actual end of the file.

Precious execution in bash

zcat file.txt.gz | txtToBinary | binaryToOutput

      

Command line execution

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 one ^Z

as the end of the file.

Tried so far

My solutions were as follows: when compiling on Windows with C ++

Command line execution

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 also tried Cygwin which I have in some of the answers. But it also failed.

StackOverflow Answers

When looking for an answer here on SO, we see Windows , Windows EOF , Binary Solution , Binary Mode and Data Stream Completed at Byte 26, and Reaching Early Windows EOF . They tell me that:

- Windows keys (CTRL + Z, ^ Z) end the file

- I need to read in binary

+3


source to share


2 answers


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

_setmode (0, _0_BINARY);



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

+3


source


fread () is part of stdio. What you are doing is opening the source file as binary and then doing standard text mode I / O.



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

+1


source







All Articles