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); }
Related Posts:
- Data argument not used by format strings in C
- How can you print multiple variables inside a string using printf?
- Is there a way to have printf() properly print out an array (of floats, say)?
- How do you allow spaces to be entered using scanf?
- How to format strings using printf() to get equal length in the output
- How to format strings using printf() to get equal length in the output
- lseek/write suddenly returns -1 with errno = 9 (Bad file descriptor)
- What do \t and \b do?
- write() to stdout and printf output not interleaved?
- How many spaces for tab character(\t)?
- Correct format specifier for double in printf
- Scanf/Printf double variable C
- Strings and character with printf
- printf() formatting for hexadecimal
- Printing hexadecimal characters in C
- What is the argument for printf that formats a long?
- What is the printf format specifier for bool?
- C read file line by line
- How to printf “unsigned long” in C?
- How I can print to stderr in C?
- Is there a printf converter to print in binary format?
- C read file line by line
- C read file line by line
- What does “%.*s” mean in printf?
- How to print a char array in C through printf?
- waitpid, wnohang, wuntraced. How do I use these
- How to initialize array to 0 in C?
- How to do scanf for single char in C
- Sign extend a nine-bit number in C
- Warning: assignment from incompatible pointer type
- Break statement not within loop or switch in C
- How to properly malloc for array of struct in C
- Cache Simulator in C
- C: error: expected ‘)’ before ‘;’ token
- Where is the C auto keyword used?
- make: Nothing to be done for `all’
- How to printf a memory address in C
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - expected expression before ‘{‘ token
- How can one print a size_t variable portably using the printf family?
- Setting std=c99 flag in GCC
- C Vector/ArrayList/LinkedList
- Convert char array to a int number in C
- Bind failed: Address already in use
- Warning/error “function declaration isn’t a prototype”
- What is the difference between %f and %lf in C?
- Returning an array using C
- %i or %d to print integer in C using printf()?
- warning: initializer element is not computable at load time
- What is stdin in C language?
- How to prevent multiple definitions in C?
- How to pass 2D array (matrix) in a function in C?
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- c – warning: implicit declaration of function ‘printf’
- Flushing buffers in C
- What are 0x01 and 0x80 representative of in C bitwise operations?
- How to make parent wait for all child processes to finish?
- error: aggregate value used where an integer was expected
- Undefined Reference issues using Semaphores
- What is time(NULL) in C?
- Use of cudamalloc(). Why the double pointer?
- C dynamically growing array
- Why should we check WIFEXITED after wait in order to kill child processes in Linux system call?
- How to compile makefile using MinGW?
- Mapping a numeric range onto another
- Why are hexadecimal numbers prefixed with 0x?
- Portable way to check if directory exists [Windows/Linux, C]
- OpenGL — GL_LINE_LOOP —
- too many arguments for format [-Wformat-extra-args]
- How to use EOF to run through a text file in C?
- Incorrect checksum for freed object on malloc
- Valgrind complains with “Invalid write of size 8”
- How to print an unsigned char in C?
- Read and write to binary files in C?
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- How to copy a char array in C?
- Simple way to check if a string contains another string in C?
- What does “request for member ‘*******’ in something not a structure or union” mean?
- cast to pointer from integer of different size, pthread code
- Invalid type argument of unary ‘*’ (have ‘int’) Error in C
- Segmentation fault (core dumped) due to fgets – I think
- Why do I get “cast from pointer to integer of different size” error?
- C subscripted value is neither array nor pointer nor vector when assigning an array element value
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Unknown ending signal when using debugger gdb
- Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio
- error: too few arguments to function `printDay’ (C language)
- How to convert integer to char in C?
- Is there a good Valgrind substitute for Windows?
- Difference between sizeof(char) and sizeof(char *)
- Where to find the complete definition of off_t type?
- struct has no member named
- Difference between “move” and “li” in MIPS assembly language
- printf not printing to screen
- Initializing array of structures
- Still Reachable Leak detected by Valgrind
- How to empty a char array?
- Assembly x86 – “leave” Instruction
- What is the difference between array and enum in C ?