/* 
* Code Snippets for this week
* Small programs to show bits of C and bits of C. 
* >> I've tried to put * at the start of all the comment lines. 
*/

/*
* Declare a file 
*/
FILE *myFileF;

/* 
* Opening and closing files (options - (r)ead, (w)rite, (a)ppend
*/

myFileF = fopen("filename.txt","wa");

/* Lots of code reading and writing

fclose(myFileF);

/* Reading and writing to file */

int myNumber = 6;

/*just like printf but the first parameter is a FILE* */
fprintf(myFileF,"%d\n", myNumber);

/* Note the & in this one, and we don't put a carridge return after the %d */
fscanf("myFileF,"%d", &myNumber;


/* Working with files - short example */

#include <stdio.h>

int main(char* argv, int argc)
{
    const int BUFFER_MAX = 64;
    FILE* myFile;
    
    myFile = fopen("aFilename.txt","w");

    fprintf(myFile,"Hello World\n");

    fclose(myFile);
    
    char buffer[BUFFER_MAX];

    /* Re-use the same pointer but re-allocate 
     * with another fopen 
     */

    myFile = fopen("aFilename.txt", "r");

    fscanf(myFile,"%s",buffer);

    /* could create a buffer the right 
     * size to create a ragged array here 
     */
    
     fprintf("the file contained: %s \n", buffer);

     fclose(myFile);

     exit(0);
}

