manipulare stdin
-
Hello everybody, I'm creating an application for a barcodescanner-device. In the main-thread, a
gets(buffer);
waites for user key-input. In a separated thread I poll on the barcode-scanner. If the user scans a barcode, I extract the barcode and want to place it into the stdin-buffer. Then, the gets()-Function should see the (new) data into the stdin and returns like a real key-input. I tried it already this way :
fscanf(stdin,"%s",barcode);
fflush(stdin);but the gets(buffer); still waites for user input. Is it possible to manipulate the imput buffer in this way? Big thanks for any help :)
-
Hello everybody, I'm creating an application for a barcodescanner-device. In the main-thread, a
gets(buffer);
waites for user key-input. In a separated thread I poll on the barcode-scanner. If the user scans a barcode, I extract the barcode and want to place it into the stdin-buffer. Then, the gets()-Function should see the (new) data into the stdin and returns like a real key-input. I tried it already this way :
fscanf(stdin,"%s",barcode);
fflush(stdin);but the gets(buffer); still waites for user input. Is it possible to manipulate the imput buffer in this way? Big thanks for any help :)
I guess u can not write in input stream and you can not read from output stream. However it looks like you have two threads one is reading Barcode from device and you want to pass it into another thread. Why you are using gets for that? You have two ways. 1. Start second thread after reading the Barcode. Pass barcode as an argument of thread or 2. If you can not pass then you can use global variable.
-
Hello everybody, I'm creating an application for a barcodescanner-device. In the main-thread, a
gets(buffer);
waites for user key-input. In a separated thread I poll on the barcode-scanner. If the user scans a barcode, I extract the barcode and want to place it into the stdin-buffer. Then, the gets()-Function should see the (new) data into the stdin and returns like a real key-input. I tried it already this way :
fscanf(stdin,"%s",barcode);
fflush(stdin);but the gets(buffer); still waites for user input. Is it possible to manipulate the imput buffer in this way? Big thanks for any help :)
As already stated, you cannot write to the read-only stdin stream. (Check the return code of fscanf.) You could try the following:
std::stringbuf buffer;
std::iostream stream(&buffer);You can both read and write to that stream. You can have one thread collecting data from stdin, and one thread to collect data from elsewhere. This stream will not be threadsafe however, so you have to add locks to read and write access. I suppose you can do something similar in C if C++ is not your choice.
-
I guess u can not write in input stream and you can not read from output stream. However it looks like you have two threads one is reading Barcode from device and you want to pass it into another thread. Why you are using gets for that? You have two ways. 1. Start second thread after reading the Barcode. Pass barcode as an argument of thread or 2. If you can not pass then you can use global variable.
-
As already stated, you cannot write to the read-only stdin stream. (Check the return code of fscanf.) You could try the following:
std::stringbuf buffer;
std::iostream stream(&buffer);You can both read and write to that stream. You can have one thread collecting data from stdin, and one thread to collect data from elsewhere. This stream will not be threadsafe however, so you have to add locks to read and write access. I suppose you can do something similar in C if C++ is not your choice.