Friendly Foe
Friendly Foe

Reputation: 21

Program doesn't output text to console

I'm using an UNIX online terminal to write this code. The program compliles successfully but it won't output anything to the console. It seems to ignore printf() and putchar instructions

if(pid > 0) 
{
    file = open("comenzi.txt", O_WRONLY);
    read(file, ch, sizeof(ch));
    printf("%s", ch);
    write(fd[1], ch, sizeof(ch));
    close(fd[1]);
    close(file);
}
else { //procesul fiu
    while(read(fd[0], &rd, 1) > 0);
    putchar(rd);
    close(fd[0]);
}

How do I make it output text to console? Thanks.

Upvotes: 2

Views: 115

Answers (1)

Daniel Walker
Daniel Walker

Reputation: 6740

You're opening file in write-only only mode and yet you're attempting to read from it. Therefore, your call to read will fail and therefore you're not writing anything meaningful to stdout. Depending on how ch was initialized, you could be writing exactly nothing.

You need to change O_WRONLY to O_RDONLY.

Upvotes: 4

Related Questions