EmbLogic's Blog

Read Write driver

Application program for read/write

#include<stdio.h>
#include<fcntl.h>       // For open()
#include<unistd.h>      // For read()
#include<string.h>

int main(int charc, char * argv[])
{
int fd;
char wbuf[40];
char rbuf[40];
ssize_t rd,wr;

strcpy(wbuf,”HELLO KERNEL!!\n”);
strcpy(rbuf,””);
fd = open(argv[1], O_RDWR);
printf(“File Descp. = %d\n”,fd);

wr = write(fd, wbuf, sizeof (wbuf));
printf(“\nData in file is: \n%s\n”,wbuf);

fd = open(argv[1], O_RDWR);
printf(“File Descp. = %d\n”,fd);

rd = read(fd, rbuf, sizeof (rbuf));
printf(“\nrd = %d \n”, rd);
printf(“%s\n”, rbuf);

close(fd);
printf(“File closed”);
return 0;

}

3 Responses to Read Write driver

  1. Manjeet says:

    why u used open() twice..????????

  2. spatlou says:

    Once for writing: As the node is unidirectional… if the node is used for writing once then from then onwards it could be used for writing only. Now, if you want to read you need a new node in the read mode. so it has to be reopened in the read mode.

  3. rakhi says:

    Even i can’t understand why the file is opened two times,when you can use same file descriptor for read(),write() operation…using syn between these two e.g. pipes…which is named pipes…

Leave a Reply to Manjeet Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>