C Tutorial
C Files
FILE pointers read and write files. Always check that fopen succeeded before you use the stream.
A FILE pointer is a stream
printf writes to the screen. Files keep data after the program ends. In C you include<stdio.h>. fopen returns a FILE *. fprintf writes.fscanf reads. fclose releases the stream.
Always test the pointer. If fopen fails, it returns NULL. Do not callfprintf on that value.
stdout is a FILE too
printf is a convenience for writing to stdout. fprintf(stdout, ...) does the same job. This program does not need a disk. Use it to confirm the chapter examples open in/c/try.
Example
#include <stdio.h>
int main(void) {
fprintf(stdout, "hello from fprintf\n");
printf("hello from printf\n");
return 0;
}fopen, check, then use
Pass a path and a mode. "w" creates or overwrites a text file. "r" opens an existing file for reading. If the call fails, print an error and skip the work.
Example
#include <stdio.h>
int main(void) {
FILE *fp = fopen("notes.txt", "w");
if (fp == NULL) {
printf("could not open notes.txt for writing\n");
return 1;
}
fprintf(fp, "first line\n");
fprintf(fp, "second line\n");
fclose(fp);
printf("wrote notes.txt\n");
return 0;
}On a machine with a filesystem this creates notes.txt in the working directory. If the open fails, you should see the error message. That is the check doing its job.
Write, then read the same file
Close after writing. Open again with "r". Check fopen a second time. Thenfscanf can pull values back. Doing both in one main means you do not depend on a file that already existed.
Example
#include <stdio.h>
int main(void) {
FILE *fp = fopen("notes.txt", "w");
if (fp == NULL) {
printf("could not open notes.txt for writing\n");
return 1;
}
fprintf(fp, "%d %d\n", 3, 4);
fclose(fp);
fp = fopen("notes.txt", "r");
if (fp == NULL) {
printf("could not open notes.txt for reading\n");
return 1;
}
int a = 0;
int b = 0;
if (fscanf(fp, "%d %d", &a, &b) == 2) {
printf("%d\n", a + b);
}
fclose(fp);
return 0;
}If both opens succeed, the program prints 7. fscanf returns the number of items assigned. Check that count the same way you check scanf.
Open, check, close
| Call | Role |
|---|---|
fopen(path, "w") | Create or overwrite, then fprintf |
fopen(path, "r") | Open existing, then fscanf |
fp == NULL | Open failed — do not use fp |
fclose(fp) | Flush and release the stream |
- Always test
fopenbefore reading or writing. - A failed open is not a crash if you handle it. Using a
NULLFILE *is undefined. - Paths are relative to the process working directory unless you pass an absolute path.
Paste the write-then-read listing into /c/try. That is the C compiler, not If the sandbox has no disk, theNULL check prints the error instead of crashing.
Next: typedef, so a struct or a function pointer can have a shorter name.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Chemistry
A titration table on stdout
Two titres 24.8 mL and 25.1 mL are typical concordant-ish burette readings. You would still average only the ones that agree within 0.1 mL in a real write-up. This listing only records them.
fprintf can target a disk file or stdout. Using stdout means /c/try can show the report without a filesystem.
Example
#include <stdio.h>
int main(void) {
fprintf(stdout, "titration, HCl vs NaOH\n");
fprintf(stdout, "run 1: 24.8 mL\n");
fprintf(stdout, "run 2: 25.1 mL\n");
return 0;
}Physics
A drop-test CSV
s = ½ g t² again: t = 0, 1, 2 s maps to 0, 4.91, 19.62 m. A CSV is how you later plot that in a spreadsheet. fopen "w" creates or overwrites. If the open fails, print that fact; do not fprintf on NULL.
Example
#include <stdio.h>
int main(void) {
FILE *fp = fopen("drop.csv", "w");
if (fp == NULL) {
printf("could not write drop.csv\n");
return 1;
}
fprintf(fp, "t_s,s_m\n");
fprintf(fp, "0,0\n");
fprintf(fp, "1,4.91\n");
fprintf(fp, "2,19.62\n");
fclose(fp);
printf("wrote drop.csv\n");
return 0;
}