A file is a dynamic structure located in the secondary memory.
The C language allows the following file operations:
– like text – such a file contains a line of lines separated by a newline (‘\ n’)
– by binary – such a file contains a sequence of bytes without any structure.
Editing a file involves associating it with an I/E channel called stream or stream.
Three predefined channels open automatically when you launch a program:
1. stin – input file, text, is the standard input – the keyboard.
2. stdout – output file, text, is standard output – monitor screen.
3. stderr – output file, text, is the standard output where the error messages – screen are written.
In order to process a file, the following steps must be taken:
–designs a FILE * variable to access the file; FILE is a structure type defined in <stdio.h>, which contains information about the file and the data transfer buffer between the central memory and the file (address, buffer length, file usage mode, endpoint indicator, position in file).
–opens the file for a particular access mode using the fopen library function, which also makes the association between the file variable and the external name of the file
– the read/write file is processed with the specific functions.
– close the file using the fclose library function.
FILE * fopen (const char * filename, const char * mod);
opens the filename for mod access.
Returns pointer to file or NULL if the file can not be opened; the returned value is stored in the file variable that was declared to access it.
The opening mode can be:
– “r” – read-only from an existing file
– “w” – write, create a new file, or if it already exists, destroy the old content
– “a” – append, open for writing an existing file (the writing will be continued if the information already exists in the file, so the access pointer is placed at the end of the file)
– “+” – allows writing and reading – updating (ex: “r +”, “w +”, “a +”). A reading can not be directly followed by writing and each other. First, the slider must be repositioned by a fseek call.
– “b” – specifies a binary file
– “t” – specifies a text file (default) that automatically converts CR-LF (“\ n \ f”) into or from CR (‘\ n’).
int fclose (FILE * pFile);
– closes the file associated with the pFile variable and frees the buffer; returns 0 to success, EOF (end of file) error
int fseek (FILE * pFile, long offset, int whence)
repositions the pointer associated with the pFile file; offset – the number of bytes between wherece positions.
wherece – has one of three possible values:
SEEK_SET = 0 – Search is from the beginning of the file
SEEK_CUR = 1 – Search from current position
SEEK_END = 2 – Search at the end of the file
long ftell (FILE * pFile);
returns the current position within the file associated with pFile.
int fgetpos (FILE * pFile, fpos_t * ptr);
this function stores the current position in the variable ptr within the file associated with pFile (to be used later with the fsetpos function).
int fsetpos (FILE * pFile, const fpos_t * ptr);
this function sets the current position in the file associated with pFile to ptr, previously obtained by the fgetpos function.
int feof (FILE * fis);
returns 0 if no end of file has been detected at the last reading operation, ie a non-zero (true) value for the end of the file.
FILE * freopen (const char * filename, const char * mode, FILE * fp);
closes the fp file, opens the filename in mode and associates it to fp; returns fp or NULL in case of error.
int fflush (FILE * fp);
This function is used for open files for writing and has the effect of writing to the file the data in its associated buffer, which has not yet been filed.