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

Dani AI

Generated

The symptoms reported by (black boxes/question-marks, empty printf/puts) point to two common mistakes: treating arbitrary bytes as a C string without a terminating NUL, and not using the actual byte counts returned by read()/write(). was right to ask for return values — those numbers are the authoritative counts and must drive any string-termination or subsequent writes. Also watch for off-by-one errors when counting file length (fgetc loops can easily add one too many) and simple typos when closing descriptors.

A robust, portable writer pattern is to stream the file in chunks and handle partial writes explicitly:

/* writer: stream file -> FIFO in chunks (handle partial writes) */
#define CHUNK 4096
char buf[CHUNK];
size_t r;
while ((r = fread(buf, 1, CHUNK, in)) > 0) {
    size_t off = 0;
    while (off < r) {
        ssize_t w = write(fd, buf + off, r - off);
        if (w <= 0) { perror("write"); goto cleanup; }
        off += (size_t)w;
    }
}

On the reader side, always use the read() return value and terminate (or print exactly N bytes). Never call puts/printf("%s") on an un-terminated buffer:

/* reader: check return, terminate before using string APIs */
ssize_t n = read(fd, buffer, sizeof buffer - 1);
if (n > 0) {
    buffer[n] = '\0';
    puts(buffer);            /* safe now */
    /* or for binary data: write(STDOUT_FILENO, buffer, n); */
}

Short checklist and tips: (1) To allocate exactly sized buffers use stat() or fseek/ftell + malloc(sz+1) and set buf[sz] = '\0'. (2) Handle partial writes in a loop. (3) Check all return values (open/fopen/mkfifo/read/write/close) and print perror on error. (4) Remember FIFO semantics: open(O_WRONLY) blocks until a reader exists (O_NONBLOCK can change that). (5) For large/unknown files, streaming in chunks avoids resizing complexity; realloc is fine if a single-buffer approach is preferred. These changes address the root causes seen in the thread and prevent intermittent behavior that sometimes "appears to work" only after adding debug prints.

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.