Hello Community

i am using Linux (fedora) gcc compiler.
Normally i use Java but for the Interprocess communication part we got C.

My problem specifically is that when I read from a file with the fread() function
and save it in a char buff[somesize] I cant use printf() to display it in the console
so I used puts() to test if fread() did what it should.

When I use a named pipe to send the char array to another programm reading from that pipe
the function puts() and printf() in the programm reading the pipe display nothing in the console

I already wrote a programm where i use gets() to get a string from the console and send it and that worked flawlessly.

I ditched error handling and whatnot to keep the programm as simple as possible.
I have 2 terminals and start the programms seperatly.
It has to be some silly mistake or stream/buffer stuff I don't understand because for the life of it Google could not find anyone with the same problem

read and send:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

int main(void) {

char buf[1024];

FILE *in;
int fd;

int ch = 0;
int count = 0;


in = fopen("start.c","rb");
fd = open("FIFO", O_WRONLY);

while (ch!=EOF) {
		ch = fgetc(in);
		++count;
		}
rewind(in);

for(; ;) {
	
	size_t n = fread (buf, 1, count, in);
	buf[n] = '\0';
	
	if(n<count)
		break;
	}
write(fd,buf,count);
colse(fd);

return 0;
}

get and display

#include <stdio.h>
#include <fcntl.h>
#include <string.h>


int main() {

	char buffer[1024];
	int fd;

	mkfifo("FIFO", 0777);		
	fd = open("FIFO", O_RDONLY); 
	read( fd, buffer, 300 );

	puts(buffer);
    printf("Ausgabe: %s \n",buffer);
		
	close(fd);
        unlink("FIFO");

return 0;
}

Thank you
notdoppler

Recommended Answers

All 5 Replies

Would you post up a small sample of the input file, which is giving you this problem. I'm having trouble visualizing what the problem could be.

Hey Adak

right now i just read in the sourcecode from my start programm ("start.c") and I tried a plain text file fileld with some random chars but here is the start.c

thats the file better yet the programm i use to start my 2 prozesses

and further information the output in the terminal are these black karos with white question marks

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




int main() {

        int res;
        int pid;
        int ret_pid;
        int status;

        unlink("FIFO");

        printf("Creating PiPe \n");
        res =mkfifo("FIFO", 0777);
        if(res !=0) 
        { 
                printf("Error in creating Pipe \n");
                exit(1);
        }

        if(( pid = fork()) == 0 )
        {
                if( execlp( "gnome-terminal", "gnome-terminal",
"--disable-factory","50x25+0+0","-e","./pipe_empf", NULL) < 0 )
                {
                        printf("Sohn: \t Fehler beim Starten eines Xterm");
                        exit( -1 );
                }
        }

        if(( pid = fork()) == 0 )
        {
                if( execlp( "gnome-terminal", "gnome-terminal",
"--disable-factory","50x25+0+0","-e","./pipe_send", NULL) < 0 )
                {
                        printf("Sohn: \t Fehler beim Starten eines Xterm");
                        exit( -1 );
                }
        }

        int i;
        for(i=0; i<2; i++) 
        {
                ret_pid = wait( &status );
        }

        unlink("FIFO");

        return 0;

}

The return value for the read and write system calls is the number of char successfully read/ written.
Print that value and post the result here

Hi abhimanipal

I did as you asked and you are on to something.

And i have to say it is getting wierd because now that i added the return value it ... works and i have not the littelest idea why ... because the only thing i changed is that i ask the return value now.

read and write values were 908 whhat is exactly the number of bytes my little count loop gives me

Followup question

if such a thing is allowed my start.c file has 908 bytes what hits a bit close to the 1024 i had reserved is there a way so that i can make my char array dynamic in the way that it resizes itself if necessary ??

notdoppler

PS: if there is a thread for something like this a link would be nice but i will search for it anyway sometime tomorrow
my original question can be flagged as solved

I dont think you can make your char array dynamic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.