From Matthew Probert · 1996-04-02 12:13 · #75185 in R25C
C FILE I/O
Matthew Probert
Servile Software
BASIC FILE I/O
There are four very basic file operations: open, read, write and close.
The standard C library provides four basic functions for these four basic
operations: fopen(), fgets(), fputs() and fclose() respectively. They are all
defined in the header file 'stdio.h' and each operate with 'buffered streams',
often referred to as just 'streams'.
A stream is a hardware independent process for accessing different type of
device and is identified by the variable type 'FILE', again defined in the
header file 'stdio.h'
To open a stream C provides the function fopen() which accepts two parameters,
the name of the file to be opened, and the access mode for the file to be
opened with. The access mode may be any one of;
Mode Description
r Open for reading
w Create for writing, destroying any existing file
a Open for append, creating a new file if it doesn't
exist
r+ Open an existing file for reading and writing
w+ Create for reading and writing, destroying any
existing file
a+ Open for append, creating a new file if it doesn't
exist.
Additionally the transfer mode may be defined by appending 'b' or 't' to the
access mode string. A value of 'b' will cause the stream to be opened in
binary mode, and a value of 't' will cause the stream to be opened in text
mode. To open and create a new file in binary mode (no translation) with
read/write access you would use:
fopen("myfile.bin","wb+");
If successful, fopen() returns a FILE pointer, if fopen() fails to open the
file, it returns a value of NULL (defined in stdio.h). The prototype for
fopen() is:
FILE *fopen(const char *s,const char *access);
fgets() reads a string from a previously opened stream. It takes three
parameters; a pointer to a string buffer, an integer count of how many
characters to read, and finally a FILE pointer to the open stream. The
prototype for fgets() looks like this:
char *fgets(char *s, int numbytes, FILE *fp);
fgets() stops reading either when numbytes - 1 bytes have been read, or a
line feed character is read in. A null terminating byte is appended to the
read string, s. If an error occurs, fgets() returns NULL.
fputs() writes a null terminated string to a previously opened stream. It
takes two parameters; a character pointer to the string to be written and a
FILE pointer to the open stream. The prototype for fputs() looks like this:
int fputs(char *s, FILE *fp);
If an error occurs in writing to the stream, fputs() returns a value of EOF
which is defined in stdio.h.
The following example illustrates file I/O at its simplest:
#include <stdio.h>
main()
{
FILE *fp;
int result;
/* Create a file called 'myfile.txt' and open it read/write access */
fp = fopen("myfile.txt","w+");
/* Check the result of the open function */
if (fp == NULL)
printf("\nUnable to open file\n");
else
{
/* Write to the file */
result = fputs("Hello file!",fp);
/* Check result of write */
if (result == EOF)
printf("\nError writing to file.");
/* Close the file */
fclose(fp);
}
}
Sometimes you may not wish to transfer entire strings to and from a file. You
may wish to transfer individual characters instead. The standard C library
provides two functions similar to fgets() and fputs() to allow this single
byte transfer.
fgetc() accepts a single parameter, a FILE pointer to an open stream, and
returns a single byte read from the stream, or EOF if an error occurred.
fputc() accepts two parameters, the value of the byte to write and a FILE
pointer to an open stream. It returns EOF if an error occurred writing to the
stream.
Formatted I/O may be achieved with fprintf() and fscanf(). These are standard
library C functions related to printf() and scanf() except that they take an
additional parameter, an open stream. The prototype for fprintf() is:
int fprintf(FILE *fp, const char *format[,argument, ...]);
fprintf() attempts to write to the specified stream. If an error occurs, it
returns the value EOF.
fscanf() has the prototype:
int fscanf(FILE *fp, const char *format[,address, ...]);
fscanf() attempts to read from the specified stream a series of input fields.
fscanf() returns the number of input fields read and stored.
The following program illustrates the use of fprintf() to write formatted data
to a file:
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int n;
char string[20];
double f;
fp = fopen("data.txt","w+");
if (fp == NULL)
printf("\nUnable to create data file");
else
{
strcpy(string,"Text");
n = 123;
f = 56.78;
if (fprintf(fp,"%s %d %lf",string,n,f) == EOF)
printf("\nUnable to write to file");
fclose(fp);
}
}
This data may then be read back from the file by a similar program, but this
time using fscanf(). Notice the difference in the fopen() command this time!
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int n;
char string[20];
double f;
fp = fopen("data.txt","r");
if (fp == NULL)
printf("\nUnable to open data file");
else
{
if(fscanf(fp,"%s %d %lf",string,&n,&f) != 3)
printf("\nUnable to read file");
fclose(fp);
printf("\nReceived: %s %d %lf",string,n,f);
}
}
... [Servile Info Journal]
___ Blue Wave/QWK v2.12
--- WILDMAIL!/WC v4.12
* Origin: Ooh! LONDON [LiveChat-MultiLines] 0181-395-3108 (2:254/233.0)