Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Windows program from interpreting ^Z as end of file

Tags:

c++

c

linux

windows

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

like image 615
eleijonmarck Avatar asked Dec 28 '25 10:12

eleijonmarck


2 Answers

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.

like image 80
eleijonmarck Avatar answered Dec 30 '25 22:12

eleijonmarck


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().)

like image 33
Mike Crawford Avatar answered Dec 30 '25 22:12

Mike Crawford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!