I'm having this fifo example in which the child process sends an integer to the parent process. I want it to send a string instead, but it doesn't work.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
void errexit(char *errMsg){
printf("\n About to exit: %s", errMsg);
fflush(stdout);
exit(1);
}
int main()
{
char ret;
pid_t pid;
char value;
char fifoName[]="/tmp/testfifo";
char errMsg[1000];
char input_string[30];
scanf("%s", input_string);
FILE *cfp;
FILE *pfp;
ret = mknod(fifoName, S_IFIFO | 0600, 0);
/* 0600 gives read, write permissions to user and none to group and world */
if(ret < 0){
sprintf(errMsg,"Unable to create fifo: %s",fifoName);
errexit(errMsg);
}
pid=fork();
if(pid == 0){
/* child -- open the named pipe and write an integer to it */
cfp = fopen(fifoName,"w");
if(cfp == NULL)
errexit("Unable to open fifo for writing");
ret=fprintf(cfp,"%s",input_string);
fflush(cfp);
exit(0);
}
else{
/* parent - open the named pipe and read an integer from it */
pfp = fopen(fifoName,"r");
if(pfp == NULL)
errexit("Unable to open fifo for reading");
ret=fscanf(pfp,"%s",&value);
if(ret < 0)
errexit("Error reading from named pipe");
fclose(pfp);
printf("This is the parent. Received value %d from child on fifo \n", value);
unlink(fifoName); /* Delete the created fifo */
exit(0);
}
}
I'm getting an error at 'ret=fscanf(pfp,"%s", value);', saying that %s expects char but value is type int, even if it isn't. it's declared as char.
What am I missing? I'm just trying to send a string to the parent and print it in there.