Reputation: 2673
I wrote this code to get my hands working on the system calls. I expected the first printf to show result in the console.. but both of them are showing the the given file location..
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main(){
printf("Hello World!!! Before close\n");
close(1);
int fd = open("/home/abhishek/Desktop/example.txt",O_RDWR);
printf("Hello World!!! After close");
}
Where actually am going wrong??
Upvotes: 2
Views: 507
Reputation: 992717
The output to stdout
may be buffered, and not actually written to the underlying file descriptor until the stream is flushed or closed. Try:
fflush(stdout);
before close(1)
.
Note that mixing stdio and system file descriptor operations is not generally a good idea. Use one or the other, otherwise you may get confusing behaviour like you have shown.
Upvotes: 2