I am opening a file with open(), and need to print to that file using printf with no output to the console. How do I do this? I can successfully create the file, and printf to the console, but that is not correct.
int main(int argc, char *argv[]) {
int fd;
char *name = "helloworld";
fd = open(name, O_CREAT);
char *hi = "Hello World";
printf("%s\n", hi);
close(fd);
exit(0);
}
I need the program to have no output to the console, but if I look at the file helloworld
, it should have "Hello World" written inside. Such as:
prompt> ./hello
prompt> more helloworld
Hello World
There's a trick to this.
You need to duplicate the open file descriptor to file descriptor 1, i.e. stdout
. Then you can use printf
:
int main(int argc, char *argv[]){
int fd;
char *name = "helloworld";
fd = open(name, O_WRONLY | O_CREAT, 0644);
if (fd == -1) {
perror("open failed");
exit(1);
}
if (dup2(fd, 1) == -1) {
perror("dup2 failed");
exit(1);
}
// file descriptor 1, i.e. stdout, now points to the file
// "helloworld" which is open for writing
// You can now use printf which writes specifically to stdout
char *hi = "Hello World";
printf("%s\n", hi);
exit(0);
}
If you open a file with open()
then you get an int
file descriptor which you can write to with write()
.
If you open a file with fopen()
then you get a FILE*
handle and can use the stdio family of functions to write to it, which includes fprintf()
and fputs()
.
These are two different levels of abstraction. open()
doesn't mix well with the printf()
family. You could get them to work together with fdopen()
, but it's not a good idea to mix abstractions.
You can close the standard out before you use printf, so that the output would be saved in the file directory you opened instead of printing to the console.
close(1); // close(STDOUT_FILENO);
fd = open(name, O_CREAT);
char *hi = "Hello World";
printf("%s\n", hi);
close(fd);
I'm assuming the printf() command can only print the output to one directory at one time and it would go to the default standard output directory if you do not close it first. If you find out a way to set the output directory to the fd without closing the STDOUT_FILENO, please let me know.