Monday, February 8, 2016

Wap a menu driven programe to store some information inside a file and read information from file and print it on LCD screen.

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define  NODE_EXP                      "/dev/fpga_char_lcd"   /* Device name */
#define  LCD_ON                          1 
#define  LCD_CLEAR                    2
#define  LCD_SET_HOME            3
#define  LCD_CURSOR_BLINK    4
#define  LCD_DATA_WRITE         5
#define  LCD_OFF                        6

/* Application : To test the functionaity of file handling */


int main (int argc,char *argv[])
{
 int fd= 0;
 size_t sw,sk,choice;
 int exp_dev= 0; /* exp_dev is file descriptor */
 int ret,res= 0;  /* return value of system call */
char p[]="This is my first programme of file handling in mtlc for read and           write";
char bufr[200];
printf("*************************************************\n");
printf("press 1 for write information inside file\n");
printf("press 2 for read information from file\n");
printf("Enter your choice :");
scanf("%d",&choice);
if(argc!=2)
    {
       printf("\n ERROR: Usage:: File_w <filename> \n");
       return 1;
    }
    switch(choice)
    {
    // ******************* Write message from a file ***********************//
    case 1:
    /* open as blocking mode */
    fd = open(argv[1], O_WRONLY | O_CREAT);
    if (fd < 0){
    printf("error opening");
    return 1;
       }
    printf("File is ready to open");
    sw = write(fd,p,sizeof(p));
    if(sw<=0)
    {
        printf("\n Zero bytes has been written ....");
        return 1;
    }
    printf("file name is %s with text is %s with size %d\n",argv[1],p,sw);
    getchar();
        close(fd);
   
  // ******************* Write message from a file ***********************//
     // ******************* Read message from a file ***********************//
    case 2:
    fd = open(argv[1], O_RDONLY);
    if (fd < 0){
    printf("error opening");
    return 1;
        }
    printf("File is ready to open");
    //sk = read(fd,bufr,sizeof(bufr));
    sk = read(fd,bufr,100);
    if(sk<=0)
    {
        printf("\n Zero bytes has been written ....");
        return 1;
    }
   
    //printf("file name is %s with text is %s with size %d",sw);
    printf("\n file name is %s with text is %s with size %d\n",argv[1],bufr,sk);
    sk = write(1,bufr,20);
    getchar();
       close(fd);
   // ******************* Read message from a file ***********************//

   // ***************** Read message on LCD screen*********************//
    exp_dev = open(NODE_EXP, O_RDWR);     
     if (exp_dev < 0){
    fprintf(stderr, "Open error: %s\n", NODE_EXP);
    return 1;
    }

                if (res=ioctl(exp_dev,LCD_ON, NULL) < 0 )      /* Ioctl */
                {
                 printf("%d---> Error in switching OFF the LCD \r\n",res);
                 getchar();
                 close(exp_dev);
                 return 1;
                }
        printf("\n LCD_ON: Hit A Key To Continue...");
        getchar();
            if (res=ioctl(exp_dev,LCD_CLEAR, NULL) < 0 )
            {
            printf("%d---> Error in Clearing the LCD \r\n",res);
            getchar();
            close(exp_dev);
            return 1;
           }
            if (res=ioctl(exp_dev,LCD_SET_HOME, NULL) < 0 )
            {
            printf("Error in Set home the LCD \r\n");
            getchar();
            close(exp_dev);
            return 1;
            }
            if (ioctl(exp_dev,LCD_CURSOR_BLINK , NULL) < 0 )
            {
             printf("Error inCursor blink in LCD \r\n");
             getchar();
             close(exp_dev);
             return 1;
             }
        if (ioctl(exp_dev,LCD_DATA_WRITE,p) < 0 )
         {
          printf("Error in switching OFF the LCD \r\n");
          getchar();
           close(exp_dev);
           return 1;
           }
        printf("\n Hit A Key To Continue...");
        getchar();
        if (ioctl(exp_dev,LCD_OFF, NULL) < 0 )
        {
         printf("Error in switching OFF the LCD \r\n");
         getchar();
         close(exp_dev);
         return 1;
         }

      close(exp_dev);
      printf("\n Hit A Key To Stop...");
      getchar();
      break;
    // ********************* Read message on LCD screen*********************//
  
    default:
    printf("Sorry! invalid request \n");
    getchar();
       close(fd);
    break;

    }

    return 0;
}

No comments:

Post a Comment