stdio.h
header file.
FILE *
, is used to handle the I/O.
It’s two words, kinda sorta.
fopen
and fclose
manipulate this type and
give us access to a file.
fopen
takes two arguments and returns a handle:= fopen(
name, mode);
fopen
: fclose(
handle);
fopen
it, you fclose
it.
fopen
failed (returned NULL
), don’t call fclose
.
fclose
return an indication of whether the close succeeded,
but it’s commonly ignored.
stdio.h
defines some special constants:
NULL
— an empty pointer value
EOF
— a special number representing the end of a file
Great—we know how to open & close the file. How do we actually, you know, read or write to it?
fgetc
, fputc
, fgets
, and fputs
fopen
. They do not take the filename—that’s only
used by fopen
.
fgetc(
handle)
— gets a single character from a file.
The special value EOF
is returned if there is no more data.
fputc(char,
handle)
— puts one character into a file.
fgets(string, size,
handle)
— gets one line from a file an places it in the variable string.
fputs(string,
handle)
— writes a string to a file.
These functions keep track of where they are in the file.
The first time that you call fgets
, it reads the first line.
The second time you call it, it reads the second line, etc.
fopen
uses the filename. The other functions use the handle,
which is returned from fopen
.
#include <stdio.h> int main() { int c; // An int, not a char. FILE *handle = fopen("data","r"); // open for reading // should test if fopen failed here while ((c = fgetc(handle)) != EOF) fputc(c, stdout); fclose(handle); return 0; } % c11 fgetscEx.c % a.out This is a test of the emergency broadcast system
fgetc
into an int
?
fgetc
has to return all possible characters, and
the special value EOF
. That means that fgetc
’s value
won’t fit into a char
, so we have to upgrade it to an int
.
#include <stdio.h> int main() { char buf[80]; FILE *handle = fopen("data","r"); // open for reading // should test if fopen failed here while (fgets(buf, sizeof(buf), handle) != NULL) fputs(buf, stdout); fclose(handle); return 0; } % c11 fgetscEx.c % a.out This is a test of the emergency broadcast system
#include <stdio.h> int main() { char line[100]; // store a line here FILE *in, *out; // FILE object pointers in = fopen("infile","r"); // open for reading out = fopen("outfile","w"); // open for writing // should test if fopen failed here // read lines from the file until there are no more // fgets returns NULL if no more lines in file while (fgets(line,sizeof(line),in) != NULL) fputs(line,out); // print lines to outfile fclose(in); fclose(out); return 0; }
fprintf
and fscanf
, allow formatted I/O with a file.
fprintf(
handle, format, param, …)
= fscanf(
handle, format, &var, …)
count
is the number of variables successfully read
format
is a standard printf
-style format string
printf
(format, param, …) ≡ fprintf(stdout,
format, param, …)
scanf
(format, &var, …) ≡ fscanf(stdin,
format, &var, …)
#include <stdio.h> int main() { int a,b,c; FILE *in = fopen("infile","r"); // open for reading FILE *out = fopen("outfile","w"); // open for writing // should test if fopen failed here // read pairs of numbers from the file count = fscanf(in, "%d%d", &a, &b); while (count==2) { fprintf(out, "Sum: %d\n", a+b); count = fscanf(in, "%d%d", &a, &b); } fclose(in); fclose(out); return 0; }
fputc
, fputs
, fprintf
.
fflush(
handle);
stdio.h
defines three special handles that are always open:
stdin
— open for reading from the standard input stream
stdout
— open for writing to standard output
stderr
— open for writing to standard error
fopen
or fclose
these—it’s done for you.
stdin
or read from stdout
.
fopen
returns NULL
FILE *handle = fopen(inFilename,"r"); if (handle == NULL) { fprintf(stderr, "can't open %s\n", inFilename); exit(2); }
#include <stdio.h> int main() { char line[100]; FILE *in = fopen("innie", "r"); // open file to read if (in == NULL) { printf("Can't open input file \"innie\"\n"); return 1; } FILE *out = fopen("outie", "w"); // open file to write if (out == NULL) { // Do NOT fclose(out), because it’s not open. fclose(in); printf("Can't open output file \"outie\"\n"); return 2; } // read lines until end of file while (fgets(line,sizeof(line),in) != NULL) fputs(line,out); // write a line to outfile fclose(in); // close the file fclose(out); // close the file return 0; }