A message for Linux.com registered users: We are in the process of making changes to the Linux forums. Starting Monday, 8/13/18 at 6:00 PM PT, you will be unable to access the forums. They will re-launch as soon as possible on Wednesday, 8/15/18 with new features and improved functionality. Thank you for your patience and stay tuned for the new improved forums.

October 28, 2016

problems with read(...); and mice-device (/dev/input/mice)


i have this code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
char mice_data[3];
int mice_position(){
    int fd = open("/dev/input/mice", O_RDWR);
    if(fd == -1){
            printf("ERROR Opening %s\n", "/dev/input/mice");
        return -1;
    read(fd, mice_data, sizeof(mice_data));
    return 0; 
int mouse_x = 0;
int mouse_y = 0;
int update_mouse(){
    mouse_x += mice_data[1];
    mouse_y += mice_data[2];
    return 0;
int main(){
        printf("%i\t%i", mouse_x, mouse_y);
    return 0;

and if im running it it works, but only if the printf(\n"), statement in line 16 is there. if it isnt the program doesnt show anything.

another problem i have is that the read-functions waits until the mice-file changes/until the mouse moves. how can i solve both problems?

Click Here!