i am trying to open a file in append mode using open() api call , however following code is not working ! Its not writing anything to file! here is my code :
In order to append a new line your existing file, you need to open the file in append mode , by setting "a" or "ab" as the mode.
Move read cursor to the start of the file. Read some text from the file and check if the file is empty or not. If the file is not empty, then append '\n' at the end of the file using write() function. Append a given line to the file using write() function.
O_APPEND flag forces file pointer to point at the end of file only. so if you do lseek from start of the file, it takes the updated file pointer position as a start of file i.e. end position of old file.
The open function creates and returns a new file descriptor for the file named by filename . Initially, the file position indicator for the file is at the beginning of the file.
O_APPEND
is not a mode by itself; it's a flag. Since the value of O_RDONLY
is 0, it's like you're trying to open the file read-only but for append, which is nonsense. Use O_WRONLY|O_APPEND
or O_RDWR|O_APPEND
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With