home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13642 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.6 KB  |  60 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!wupost!cs.uiuc.edu!vela!shiva
  3. From: shiva@vela.acs.oakland.edu (SHIVAKUMAR C. BETTADAPURA)
  4. Subject: File pointer misaligned if opened in append mode.
  5. Message-ID: <1992Nov19.224117.8860@vela.acs.oakland.edu>
  6. Organization: Oakland University, Rochester MI.
  7. Date: Thu, 19 Nov 1992 22:41:17 GMT
  8. Lines: 50
  9.  
  10. I work in an ULTRIX environment. When a file is opened in append mode, the
  11. file pointer does not position itself at the end of file; if only 
  12. system calls are used. How ever, with standard library results are as 
  13. expected. 
  14.  
  15. Source code for both the versions are given below:
  16.  
  17. /* This version uses the standard library */
  18.  
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21.  
  22. main()
  23. {
  24.   FILE * fp;
  25.   if ((fp = fopen("junk","a")) == NULL)
  26.      { printf( "Create a file called junk  \n"); exit(0); }
  27.   printf("The current fpos = %ld\n", ftell(fp));
  28.   if(fwrite("junk",4, 1, fp) < 0) printf("error writing into file\n");
  29.   printf("The current fpos after write = %ld\n", ftell(fp));
  30.   fclose(fp);
  31. }
  32.  
  33. /* This uses the unix system calls */
  34.  
  35. #include <stdio.h>
  36. #include <fcntl.h>
  37.  
  38. main()
  39. {
  40.   int  fp;
  41.      if((fp = open("junk", O_WRONLY|O_APPEND)) < 0)
  42.        {
  43.     printf("Error writing into file\n");
  44.     exit(0);
  45.        }
  46.   printf("The current fpos = %ld\n", tell(fp));
  47.   if(write(fp,"junk",4) < 0) printf("error writing into file\n");
  48.   printf("The current fposafter write = %ld\n", tell(fp));
  49.   close(fp);
  50. }
  51.  
  52. Reply to:
  53. shiva@vela.acs.oakland.edu
  54.  
  55. -- 
  56. shiva
  57. --------------------------------------------------------------------------------
  58. Shivakumar C. Bettadapura        E-mail : shiva@vela.acs.oakland.edu
  59. System Programer            Office Phone # : (313) 370 - HELP 
  60.