2

I read the device file /dev/hidraw0 when in the terminal by typing cat /dev/hidraw0 as the hidraw0 was the usb mouse, the output was a sequence of characters. This sequence was generated as I moved the mouse, clicked and scrolled.

Then I also tried this with a C program. the code is,

include<stdio.h>

    int main()
    {
            FILE *ptr_file;
            char buf[1000];

            ptr_file =fopen("/dev/hidraw0","r");
            if (!ptr_file)
                return 1;

            char c;
            while (1)
            {
                c=fgetc(ptr_file);
                if(c != NULL)
                    printf("%c",c);
                else
                    printf(""); 
            }           

        fclose(ptr_file);
            return 0;
    }

this also generated the same (I suppose) sequence of characters. But in a very asynchronous manner. It didn't get updated as quickely as the cat /dev/hidraw0 command.

Why this happens ?

Lahiru Nuwan
  • 31
  • 2
  • 4

2 Answers2

1

Try to disable ptr_file buffering by calling setvbuf() with _IONBF.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Does it help if you flush the stdout after printf, fflush(stdout);?

n3rd4n1
  • 371
  • 2
  • 8