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?
- Strings and character with printf
- Printing hexadecimal characters in C
- C read file line by line
- How I can print to stderr in C?
- Is there a printf converter to print in binary format?
- What does “%.*s” mean in printf?
- How to print a char array in C through printf?
- 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
- Break statement not within loop or switch in C
- How to properly malloc for array of struct in C
- Cache Simulator in C
- make: Nothing to be done for `all’
- expected expression before ‘{‘ token
- 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
- What is the difference between %f and %lf in C?
- What is stdin in C language?
- 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’
- error: aggregate value used where an integer was expected
- How to compile makefile using MinGW?
- Portable way to check if directory exists [Windows/Linux, C]
- too many arguments for format [-Wformat-extra-args]
- Incorrect checksum for freed object on malloc
- How to copy a char array in C?
- Simple way to check if a string contains another string in C?
- cast to pointer from integer of different size, pthread code
- How to convert integer to char in C?
- Where to find the complete definition of off_t type?
- struct has no member named
- printf not printing to screen
- Initializing array of structures
- Still Reachable Leak detected by Valgrind
- What is the difference between array and enum in C ?
- What is signed integer overflow?
- C’s printf and fprintf(stdout,) are not printing
- What does the term “empty loop” refer to exactly in C and C++?
- When to use const char * and when to use const char []
- What do numbers using 0x notation mean?
- What integer hash function are good that accepts an integer hash key?
- Can I create an Array of Char pointers in C?
- Does stack grow upward or downward?
- Convert Little Endian to Big Endian
- error C2371: ‘functionname’ redefinition: different basic types
- Pause screen at program completion in C
- note: previous implicit declaration of ‘point_forward’ was here
- Does C have a string type?
- What exactly is meant by “de-referencing a NULL pointer”?
- Difference between fgets and fscanf?
- Execution of printf() and Segmentation Fault
- getc() vs fgetc() – What are the major differences?
- Can’t understand the working of getint() in C as per K&R
- Lua – Number to string behaviour
- printf format specifiers for uint32_t and size_t
- Two decimal places using printf( )
- Realloc Invalid Pointer in C
- What tools are there for functional programming in C?
- typedef fixed length array
- “-bash: gcc: command not found” using cygwin when compiling c?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- Reading float using scanf in c
- What are the differences between if, else, and else if?
- Example of waitpid() in use?
- strtok segmentation fault
- Invalid write of size 1
- How to solve static declaration follows non-static declaration in GCC C code?
- realloc(): invalid next size when reallocating to make space for strcat on char *
- C Error: declaration shadows a local variable — Won’t let me repeatedly replace the value of my float variable
- Can a function return two values?
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’
- MIPS to C Translation
- Pointer to 2D arrays in C
- How to know what the ‘errno’ means?
- Printf was not declared in this scope
- How to free memory from char array in C
- Can I get Unix’s pthread.h to compile in Windows?
- Initializing 2D char array in C
- Pointer to a string in C?
- Process exited with return value 3221225477
- gdb: No symbol “i” in current context
- Cross Platform C library for GUI Apps?
- Pre increment vs Post increment in array
- %p Format specifier in c
- “Nothing to be done for makefile” message
- Convert Char to String in C
- What is the difference between signed and unsigned int