Lseek and O_APPEND Example C Application

If you have been following the previous examples, you will note that I have only been copying a file, but not editing a file. One way to do this is to use lseek (or a sister function fseek). The below code will take an input file as the first argument, the second a file offset and the third some text to add.

  1. /**
  2.  * @file seeker.c
  3.  * @brief File seeker that takes a file (argv[1]) and will write argv[3] at the
  4.  * position specified at argv[2]
  5.  */
  6.  
  7. #define _FILE_OFFSET_BITS 64
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14.  
  15. int main (int argc, char **argv) {
  16.        
  17.         int flags = O_RDWR | S_IRUSR | S_IWUSR;
  18.         int fd = 0;
  19.         off_t off = 0;
  20.        
  21.         if((fd = open(argv[1], flags)) < 0 ) {
  22.                 perror("Unable to open the input file");
  23.                 return -1;
  24.         }
  25.        
  26.         off = atoll(argv[2]);
  27.        
  28.         if(lseek(fd, off, SEEK_SET) < 0) {
  29.                 perror("Unable to open the input file");
  30.                 return -1;
  31.         }
  32.        
  33.         if (write(fd, argv[3], strlen(argv[3])) < 0 ) {
  34.                 perror("unable to write to file \n");
  35.                 return (-1);
  36.         }
  37.        
  38.         close(fd);
  39.        
  40.         return 0;
  41. }

Now if you add the O_APPEND define to the flags argument like the bottom example - what happens?

  1. /**
  2.  * @file append.c
  3.  * @brief File seeker that takes a file (argv[1]) and will write argv[3] at the
  4.  * position specified at argv[2]
  5.  */
  6.  
  7. #define _FILE_OFFSET_BITS 64
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14.  
  15. int main (int argc, char **argv) {
  16.        
  17.         int flags = O_RDWR | S_IRUSR | S_IWUSR | O_APPEND;
  18.         int fd = 0;
  19.         off_t off = 0;
  20.        
  21.         if((fd = open(argv[1], flags)) < 0 ) {
  22.                 perror("Unable to open the input file");
  23.                 return -1;
  24.         }
  25.        
  26.         off = atoll(argv[2]);
  27.        
  28.         if(lseek(fd, off, SEEK_SET) < 0) {
  29.                 perror("Unable to open the input file");
  30.                 return -1;
  31.         }
  32.        
  33.         if (write(fd, argv[3], strlen(argv[3])) < 0 ) {
  34.                 perror("unable to write to file \n");
  35.                 return (-1);
  36.         }
  37.        
  38.         close(fd);
  39.        
  40.         return 0;
  41. }

The O_APPEND flag makes sure that the offset pointer within the file is ALWAYS at the end of the file regardless of where you placed the offset.

Blog tags: 

AttachmentSize
seeker.c760 bytes
append.c771 bytes

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.