Erik Fredericks, frederer@gvsu.edu Fall 2025
Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers
Files are treated like streams
Files are managed via file pointers
All file functions are in <stdio.h>
<stdio.h>
Generally, the process looks like this:
FILE* fp_in; fp_in = fopen("in_filename", "r"); // Read file fclose(fp_in); FILE* fp_out; fp_out = fopen("out_filename", "w"); // Write to file fclose(fp_out);
fopen
FILE* fp = fopen(filename, mode);
Modes:
"r"
"w"
"a"
"+"
You can add "b" to the end for binary mode
"b"
fopen returns NULL on failure - good to check!
NULL
When can it fail?
FILE* fp = fopen("file.txt", "r"); if(fp == NULL) { printf("Error! Could not open file\n"); return 1; }
fclose
fclose(file_pointer);
Closing your files is important!
Even if your system doesn’t buffer, we want to close files to make our code portable!
Very similar to reading input from stdin!
scanf
fscanf
fgets
getline
fscanf(fileptr, formatstr, memaddr1, …);
fgets(char* s, int size, FILE* stream);
getline(char ** lineptr, size_t n, FILE* stream);
fprintf(FILE* fp, format_str, args…);
printf
f
fputc(int c, FILE* fp);
fgetc
fputs(char* s, FILE* fp); -Writes the string to file
fputs(char* s, FILE* fp);
fread(void* buffer, size_t size, size_t n, FILE* fp);
fwrite(void* buffer, size_t size, size_t n, FILE* fp);
fp
fseek(FILE* fp, long offset, int origin);
fseek
Options for origin:
SEEK_SET
SEEK_CUR
SEEK_END
fseek: https://www.tutorialspoint.com/c_standard_library/c_function_fseek.htm
int main() { FILE *file = fopen("example1.txt", "r"); if (file == NULL) { perror("Error opening file"); return 1; } fseek(file, 0, SEEK_SET); char ch = fgetc(file); if (ch != EOF) { printf("First character in the file: %c\n", ch); } fclose(file); return 0; }
Pay attention to return values!!
EOF
If you are switch between reading and writing in the same file pointer
fseek()
fflush()
#include <stdio.h> #include <stdlib.h> int main() { FILE* fp; fp = fopen("demo.txt", "w+"); // + makes it read/write if (fp == NULL) { printf("Error opening file for writing\n"); return -1; } fprintf(fp, "%s\n", "CIS241"); fclose(fp); return 0; }
#include <stdio.h> #include <stdlib.h> int main() { FILE* fp; char buffer[255]; fp = fopen("demo.txt", "r"); if (fp == NULL) { printf("Error opening file for reading\n"); return -1; } fgets(buffer, 255, fp); printf("Data read: %s\n", buffer); fclose(fp); return 0; }
#include <stdio.h> #include <stdlib.h> int main() { FILE* fp; char buffer[255]; fp = fopen("demo.txt", "r"); if (fp == NULL) { printf("Error opening file for reading\n"); return -1; } while (fgets(buffer, sizeof(buffer), fp)) { printf("%s", buffer); } fclose(fp); return 0; }
https://stackoverflow.com/questions/29677577/writing-and-reading-from-a-file-at-the-same-time
int main(void) { FILE* filePtr = fopen("tabs.txt", "r+"); int c; while((c = fgetc(filePtr)) != EOF) { if(c == '\t') { fseek(filePtr, -1, SEEK_CUR); fputc(' ', filePtr); } } fclose(filePtr); return 0; }
https://medium.com/@future_fanatic/a-beginners-guide-to-file-handling-in-c-with-fopen-e0e7c6969b92
https://stackoverflow.com/questions/3501338/c-read-file-line-by-line
https://www.geeksforgeeks.org/c/read-a-file-line-by-line-in-c/#
Grab this file (hmm, wasn't there a terminal command you could easily use?)
Make a C program that:
Submit with your final mini-project for a bit of extra credit.
show the segfault when trying to run the read without error handling!
fseek(file, -3, SEEK_CUR); // Move 3 bytes backward from the current position
# `fseek`
char buff[10]; fgets(buff, 100, file); printf("Data: %s\n", buff);