Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. manipulare stdin

manipulare stdin

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    baerten
    wrote on last edited by
    #1

    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 :)

    R N 2 Replies Last reply
    0
    • B baerten

      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 :)

      R Offline
      R Offline
      rahul kulshreshtha
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • B baerten

        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 :)

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        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.

        home

        B 1 Reply Last reply
        0
        • R rahul kulshreshtha

          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.

          B Offline
          B Offline
          baerten
          wrote on last edited by
          #4

          Big thanks for your answer. I did it in the same way. The Main-Thread and 2 threads for the input (one for keyboard and a second one for the barcode-scanner) Greetings

          1 Reply Last reply
          0
          • N Niklas L

            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.

            home

            B Offline
            B Offline
            baerten
            wrote on last edited by
            #5

            Thanks for your answer :) I resolve it with multiple threads, like your idea ;) Greetings

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups