Advertisement
gigabytemon

C Examples: File I/O

Jun 26th, 2020
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. // FILE I/O EXAMPLE - C
  2. // (c) gigabytemon
  3. // 26/06/2020
  4. // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
  5. // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
  6.  
  7. /* introduction
  8.  * ------------
  9.  * file i/o only involves a few steps that need to be taken
  10.  * 1) declaring the file object, i.e. what we will use to refer to the file
  11.  * 2) opening the file, and in what mode (read, write, append, etc)
  12.  * 3) performing operations on the file (write to, read from)
  13.  * 4) closing the file
  14.  *
  15.  * it's important to know that some read operations will stop when it finds a space ( )
  16.  * while others will only stop at the end of the line (\n)
  17.  * also, write operations will overwrite the contents of a file unless opened explicitly
  18.  * in append mode.
  19.  */
  20.  
  21. #include <stdio.h>
  22.  
  23. void writingToFile() {
  24.  
  25.     //declare file object
  26.     FILE *fp; //this is a pointer, pointing to a FILE object that we are calling fp
  27.    
  28.     //we must open the file before we can do anything
  29.     // >> object = fopen("filename", "access mode")
  30.     fp = fopen("C:/project_files/test.txt", "a+");
  31.    
  32.     /* access modes:
  33.      * r = for reading only
  34.      * w = for writing (overwrite) only (creates file if it doesnt exist)
  35.      * a = for writing in append mode (creates file)
  36.      * r+ = for reading and writing (overwrite)
  37.      * w+ = for reading and writing (overwrite) (creates file)
  38.      * a+ = for reading and writing (append mode)
  39.      */
  40.      
  41.      //writing to file
  42.      // >> fprintf(object, "string")
  43.      fprintf(fp, "Wrote this string using fprintf. \n");
  44.      
  45.      // >> fputs("string") << doesn't need object reference
  46.      fputs("Wrote this string using fputs. \n");
  47.      
  48.      //close the file after using it
  49.      // >> fclose(object)
  50.      fclose(fp);
  51.  
  52. }
  53.  
  54. void readingFromFile() {
  55.    
  56.     //declare file object
  57.     FILE *fp;
  58.     //declare a character buffer for the strings we will read from the file
  59.     char buffer[255];
  60.    
  61.     //open the file in read mode
  62.     fp = fopen("C:/project_files/test.txt", "r");
  63.    
  64.     //reading from file
  65.     // >> fscanf(object, "%s", destination)
  66.     //NOTE: THIS WILL ONLY READ UNTIL IT ENCOUNTERS A SPACE (BLANK CHARACTER)
  67.     fscanf(fp, "%s", buffer);
  68.     printf("Line 1: %s\n", buffer);
  69.    
  70.     // >> fgets(destination, buffer size, (FILE*)object)
  71.     //NOTE: THIS WILL READ UNTIL END OF LINE
  72.     fgets(buffer, 255, (FILE*)fp);
  73.     printf("Line 2: %s\n", buffer);
  74.    
  75.     //close the file
  76.     fclose(fp);
  77.    
  78. }
  79.  
  80. void main() {
  81.     writingToFile();
  82.     readingFromFile();
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement