I have a pipe that contains a series of numbers. I will read it and at each iteration I will write what has been read in a txt file. How can I convert binary numbers to decimal numbers and record them, one per line, in my txt file?
PS The pipe is created in another file and the numbers are written using this command:
write (fp, &mynum, sizeof (mynum));
Main file
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd, fp ;
int bytesread;
char * myfifo = "myfifo";
char buf[MAX_BUF];
int count = 0;
char *filename = "memorizza.txt";
fp = open(filename,O_WRONLY | O_APPEND);
if ((fd = open(myfifo, O_RDONLY))==-1 ){ // opening pipe
perror(myfifo);
return 1;
}
while(1)
{
if((bytesread = read( fd, buf, MAX_BUF - 1)) > 0) //read pipe
{
buf[bytesread] = '\0';
count++;
write(fp , buf , MAX_BUF );
}
else {
printf("Ho ricevuto %d numeri primi \n" , count);
break;
}
}
close(fd);
close(fp);
return 0;
}