Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening file in Append mode : using open() API

Tags:

c

unix

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 :

enter image description here

like image 661
JAY G Avatar asked Aug 21 '11 04:08

JAY G


People also ask

How do I open file in append mode?

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.

How do you read and append a file in Python?

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.

Which flag is used to append the contents in the file in open () system call?

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.

What does open () do in C?

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.


1 Answers

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.

like image 87
R.. GitHub STOP HELPING ICE Avatar answered Oct 30 '22 19:10

R.. GitHub STOP HELPING ICE