/* ********************************************************************** */ /* * * */ /* * FILE: lseek.c * */ /* * * */ /* * PURPOSE: This file contains the main routines for program #1.3 * */ /* * in CS590 / Summer 2002. Specifically, this file * */ /* * shows the use of a lseek call foloowed by a write as * */ /* * opposed to using the O_APPEND flag on the open syscall. * */ /* * * */ /* * AUTHOR: Richard Johnson * */ /* * * */ /* * REVS: 05/29/2002 - Program Created. * */ /* * * */ /* ********************************************************************** */ #include #include #include #include #include /* ********************************************************************** */ /* * main - This routine opens the file "file.txt" and writes 10,000 * */ /* * lines of text to it using the lseek to end, then write. * */ /* ********************************************************************** */ int main(int argc, char **argv) { int i; /* Loop counter */ int fd; /* File descriptor */ int count = 0; /* Seek counter */ char buf[64]; /* Write buffer */ /* Check the command line args */ if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } /* Open the file for write, but without O_APPEND */ if ((fd = open("file.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { perror("open failed"); exit(1); } /* Write many lines to the file; lseek to end for each write */ for (i = 0; i < 10000; i++) { count++; sprintf(buf, "Process number %s - %d\n", argv[1], count); if (lseek(fd, 0L, SEEK_END) < 0) perror("lseek failed"); if (write(fd, buf, strlen(buf)) != strlen(buf)) perror("write failed"); } /* Cleanup */ exit(0); }