Your (first) problem lies here:
write (fd, ptr, strlen(ptr));
The strlen
of "Akshit Soni"
does not include the trailing NUL character. You need to use strlen (ptr) + 1
as the length.
You should also allow for the fact that read()
may not return all the bytes you asked for (100) nor all the bytes that were sent (12 including the NUL). It's possible (for reasons such as timing or interrupts) that only part of the data may be read by a single call to read()
.
To allow for that, you could try something like:
int main()
{
int fd;
char buff[100];
fd = open ("MyPipes",O_RDONLY);
int sz = read (fd, buff, 100);
while ((sz > 0) && (buff[sz-1] != '\0')) {
printf ("%*.*s", sz-1, sz-1, buff);
sz = read (fd, buff, 100);
}
if (sz > 0)
printf ("%s\n",buff);
close(fd);
}
As an aside, make sure you have actually created the named pipe before you run the code, with something like (from bash
):
mkfifo MyPipes