Lecture 20: Files |
FILE
fopen() fclose() fscanf() fprintf() |
feof()
fputs() fgets() fputc() fgetc() |
FILE *filehandle; |
With filehandle the variable
that
will store the pointer to the information. This is NOT equal to
the actual name of the file, as we will see in a moment. The place to
declare
this is together with the other variables.
Example:
FILE *f;
This makes f a pointer to a file
handle.
Without ever having to worry about it (no need to remember this),
the actual variable type FILE is
typedef struct{
short
level;
unsigned
flags;
char
fd;
unsigned char hold;
short
bsize;
unsigned char *buffer, *curp;
unsigned
istemp;
short
token;
} FILE;
For more information, look in your compilers help.
fopen() |
short for "file open". opens a file. |
function definition: FILE
*fopen(const char
*filename, const char *mode)
Opens a file. returns 0 if unsuccesful. |
fopen() returns a pointer to a
FILE (which we can assign to our variable of type FILE-pointer above).
fopen() takes two
parameters,
both strings (pointers to char). The first one is the name of the file
and the second one is the way it should be opened, for reading ("r"),
for
writing ("w"), or for appending ("a").
Examples:
to open a file named "OLA.TXT" for reading we use
FILE *f;
f = fopen("OLA.TXT", "r");
to open a file named "output.asc" for writing we can use
FILE *f;
f = fopen("output.asc", "w");
fscanf() |
short for "file scan fornatted". input from file |
function definition: int
fscanf(FILE *stream,
const char *format, ....)
gets fromatted input from file |
fprintf() |
short for "file print formatted". Formatted output to file |
function definition: int
fprintf(FILE *stream,
const char *format, ...)
outputs to a file. returns number of characters written, EOF if unsuccesful. |
Note that we can only use the fscanf
instructions for files that have previously been opened for input ("r")
and fprintf is only to be used
for
files opened for output ("w"). Examples:
fprintf(f, "%f", r);
fscanf(f, "%d", opcao);
|
|
|
|
fclose() |
short for "file close". Closes a file. |
function definition: int
fclose(FILE *stream)
closes a file. returns 0 if succesful. |
for example:
fclose(f);
Example:
while (!eof(f))
{
fscanf("%s", s);
}
which will read from the file until the end of
the file is encountered.
code | screen | file TEST.TXT
after running the program |
/* With File Output */
#include <stdio.h> FILE *f;
void main()
|
Name of File:
TEST.TXT |
1 Hello
2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 Hello |
code | screen | file TEST.TXT before
running the program |
/* With File Input */
#include <stdio.h> FILE *f;
void main()
|
Name of File:
TEST.TXT 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 Hello |
1 Hello
2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 Hello |
fgets() |
short for "file get string". Reads a string from file. |
function definition: char
*fgets(char s1,
int n, FILE *stream)
reads a string from file until eol (end of line), eof (end of file) or maximum of n-1 characters are read |
fgetc() |
short for "file get char". Reads a character from file. |
function definition: int fgetc(FILE
*stream)
reads a single character from file. Returns value of character if successful. |
fputs() |
short for "file put string". Writes a string to file. |
function definition: int
fputs(const char
s1, FILE *stream)
writes a string to file. Returns 0 if succesful. |
fputc() |
short for "file pu char". Writes a character to file. |
function definition: int
fputc(char
*s1, FILE *stream)
Writes a single character to file. returns value of character if succesful. |