Reputation:
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
Upvotes: 5
Views: 52764
Reputation: 431
If you use fopen
to handle this, a simpler trick is to use variadic macros, by which you can replace printf
with fprintf
in preprocessing. The ...
representing variable arguments in the macro is just like its counterpart in the declaration of functions with variable arguments. This trick works in C and C++. For example:
#include <stdio.h>
#define printf(...) fprintf(File, __VA_ARGS__)
int main()
{
FILE *File;
File = fopen("file.txt", "w+");
printf("Hello world!");
fclose(File);
return 0;
}
Note: variadic macro is used because the number of arguments of printf
is a variable.
Upvotes: -1
Reputation: 11
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.
Upvotes: 1
Reputation: 223739
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);
}
Upvotes: 5
Reputation: 361595
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.
Upvotes: 2