Advertisement
thenewboston

C Programming Tutorial - 53 - Random File Access

Aug 22nd, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     // before we could open a file and ONLY read it or ONLY write it
  7.     // what if we want to change one users name? Don't want to write entire file over
  8.     FILE * fPointer;
  9.  
  10.     // w+ open new file for writing first then reading
  11.     fPointer = fopen("bacon.txt", "w+");
  12.     fputs("I ate 3 pumpkins today", fPointer);
  13.  
  14.     // SEEK_SET means start at the beginning of the file
  15.     fseek(fPointer, 7, SEEK_SET);
  16.     fputs(" munchkins on Friday", fPointer);
  17.  
  18.     // SEEK_END means from the end of the file
  19.     fseek(fPointer, -6, SEEK_END);
  20.     fputs("top of a mountain", fPointer);
  21.  
  22.     fclose(fPointer);
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement