Advertisement
thenewboston

C Programming Tutorial - 50 - Writing Files in C

Aug 22nd, 2014
457
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.     //files allow you to save data even when you shut off your computer
  7.     //sequential access files - must write your data in order
  8.     //random access files - can store data all over
  9.  
  10.     //need this to keep track of where you are in your file
  11.     FILE * fPointer;
  12.  
  13.     //now we need to open a file to point to
  14.     //w - write (aka create) a new file
  15.     //r - read from a file
  16.     //a - append (aka add on to) end of existing file
  17.     fPointer = fopen("bacon.txt", "w");
  18.  
  19.     fprintf(fPointer, "I love cheese\n");
  20.  
  21.     //if you run this again, it will overwrite the old file
  22.  
  23.     fclose(fPointer);
  24.  
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement