LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   blocking system calls - question (https://www.linuxquestions.org/questions/programming-9/blocking-system-calls-question-632097/)

return.c 04-01-2008 02:11 AM

blocking system calls - question
 
I have a following piece of code

printf("creating log file ... ");
result = ee_log.init();
if(result == true)
{
printf("success \n");
}
else if(result == false)
{
perror("error - > Error in opening log file.");
return false;
}

but while running the program, I get "error - > Error in opening log file." printed before "creating log file ... ". Is this something to do with blocking system calls ?

It has to be like this

creating log file ...
error - > Error in opening log file.: Permission denied

But What I am getting in the terminal is

error - > Error in opening log file.: Permission denied
creating log file ...

BugZRevengE 04-01-2008 02:59 AM

Not really sure, but is perror outputting to stdout or stderr ? it may be related, because I think the printf is to stdout, and perror is to stderr... I am not sure how to change the order, but I think there would be a way. If you want you can redirect one or the other by redirecting 1> /path/stdout.txt 2> /path/stderr.txt

more info, and how to redirect both can be found here:
http://www.cpqlinux.com/redirect.html

hope this helps

Guttorm 04-01-2008 04:50 AM

It's because stdout is line buffered and stderr is not buffered at all. So the "creating log file..." will be printed when the program terminates. If you add a \n to the "creating log file..." message, the buffer will be flushed.

osor 04-01-2008 11:57 AM

Quote:

Originally Posted by Guttorm (Post 3106881)
If you add a \n to the "creating log file..." message, the buffer will be flushed.

If you don’t want the linefeed, you can force a buffer flush with
Code:

fflush(stdout);


All times are GMT -5. The time now is 02:21 AM.