diff options
author | loshprung <lshprung@scu.edu> | 2020-02-12 11:30:35 -0800 |
---|---|---|
committer | loshprung <lshprung@scu.edu> | 2020-02-12 11:30:35 -0800 |
commit | 76822cff123f3ea542449340f4b3ada51873203e (patch) | |
tree | 6797eff54af89d39658656e1b69ea87a3acb84e9 /02-12.md | |
parent | 89d105f965d422bd15b8c10afd707b4d621ab22a (diff) |
Post-class 02/12
Diffstat (limited to '02-12.md')
-rw-r--r-- | 02-12.md | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/02-12.md b/02-12.md new file mode 100644 index 0000000..ce40fa9 --- /dev/null +++ b/02-12.md @@ -0,0 +1,156 @@ +[\<- Notes 02/10](02-10.md) + +--- + +# Files Continued + +- 2 Ways: Text or Binary + +- Regardless, always open the file with `fopen`: + +``` +char filename[] = "myFile.txt"; +FILE *fp = fopen(filename, "r"); +``` + +- The above example opens the myFile.txt for reading +- The fopen() function takes in two arguments + 1. the filename + 2. the mode + - "r" -> read + - "w" -> write + - "a" -> append + - "rb" -> read binary + - "wb" -> write binary + - "ab" -> append binary + +- To read a text file: + +``` +fscanf(fp, "%d", &x); +``` + +- The above example reads the file pointed to by fp for a single integer and stores it in x +- The fscanf() function takes in three arguments + 1. the pointer to the file to read + 2. the data type to read for + 3. the memory address of the variable to save what was read + +- To write to a text file: + +``` +fprintf(fp, "%d", x); +``` + +- The above example writes the integer x to fp +- The fprintf() function takes in two arguments + 1. the pointer to the file to write to + 2. the text to write + +- To read a binary file: + +``` +fread(&x, sizeof(int), 1, fp); +``` + +- The above example reads 1 int, stores it in variable x from the file pointed to by fp +- The fread() function takes in four arguments + 1. variable address to store what is read + 2. the amount of memory to read + 3. How many times to read the amount of memory specified in the 2nd argument + 4. the file pointer to read from + +- To write to a binary file + +``` +fwrite(x, sizeof(type), 1, fp); +``` + +- The above example writes x to the file pointed to by fp +- The fwrite() function takes in four arguments + 1. The variable to write from + 2. the amount of memory being written + 3. How many times to write the amount of memory specified in the 2nd argument + 4. the file pointer to write to + +- Example: Return the number of integers in a text file greater than 100 + +``` +int count(FILE *fp){ + int number, counter = 0; + + while(fscanf(fp, "%d", &number) == 1){ + if(number > 100) counter++; + } + + return counter; +} +``` + +- Example: Return the average of all the numbers in a text file + +``` +int avg(FILE *fp){ + int number; + int sum = 0; + int counter = 0; + + while(fscanf(fp, "%d", &number) == 1){ + sum += number; + counter++; + } + + return sum/counter; +} +``` + +- Example: Copy a binary file to another file (100 bytes at a time) + +``` +FILE *source; +FILE *dest; +char srcname[] = "my_source"; //the name of my file + +source = fopen(srcname, "rb"); +dest = fopen(srcname, "wb"); +if(source == NULL || dest == NULL) return; //there was an error + +void copy_bin(FILE *src, FILE *dest){ + int r; + char x[100]; //store bytes for transfer + + while((r = fread(x, sizeof(char), 100, src)) > 0){ + fwrite(x, sizeof(char), r, dest); + } + +return; +``` + +- In that last example `r` is used instead of 100 in case there is less than 100 bytes left to read in the file +- Don't forget to `fclose` at the end! + +- Example: Write a function to compare two files, line by line + +``` +void compare(FILE *fp1, FILE *fp2){ + char *r1; //points to x1 + char *r2; //points to x2 + char x1[100]; //store from fp1 + char x2[100]; //store from fp2 + int line = 1; //what line number I'm looking at + + while(1){ + r1 = fgets(x1, 100, fp1); + r2 = fgets(x2, 100, fp2); + if(strcmp(r1, r2) != 0){ //not equal + printf("FILES ARE DIFFERENT on line %d\n", line); + break; + } + line++; + } + + if(r1 == NULL and r2 == NULL) printf("FILES ARE EQUAL\n"); + + return; +} +``` |