previous | start | next

Program to read the char device

#include <stdio.h>
#include <stdlib.h>

int main()
{
  FILE *fp;
  const int MAXLINE = 130;
  char line[MAXLINE];
  char *cp;


  for(int i = 0; i < 5; i++) {
    fp = fopen("/dev/chardev0", "r");
    if (fp == NULL) {
      printf("Unable to open device /dev/chardev0\n");
      exit(0);
    }


    cp = fgets(line, MAXLINE, fp);

    if ( cp == NULL ) {
      printf("fgets failed to read from /dev/chardev0\n");
      exit(0);
    } else {
      printf("%s\n", line);
    }
    fgets(line, MAXLINE, stdin);
    fclose(fp);
  }

  return 0;


}


previous | start | next