We can write and read blocks of data. For that, we have the functions
fread() prototype is:
unsigned fread (void *buffer, int numero_de_bytes, int count, FILE *fp);
The buffer is the memory region where the read data will be stored. The number of bytes is the size of the drive to be read. count indicates how many units to read. This means that the total number of bytes read is:
byte_number * count
The function returns the number of units actually read. This number may be less than count when the end of file is encountered or an error occurs. When the file is opened for binary data, fread can read any data type.
There must be a file in the directory before you will run the program, for example:
echo "This content will be read" >> the_file.txt
fwrite() function works like your companion
fread(), but writing to the file. Your prototype is:
unsigned fwrite(void *buffer,int byte_number,int count,FILE *fp);
The function returns the number of items written. This value will be equal to count unless an error occurs.
int remove (char *filename);
Deletes a specified file.
Standard file streams allow the programmer to read and write files in the standard way we read and wrote them on screen.
fprintf(): The fprintf() function works like the
printf()function. The difference is that the output of
fprintf()is a file and not the computer screen.
int fprintf (FILE *fp,char *str,…);
As we might expect, the only difference from the
fprintf() prototype to the
printf() prototype is the specification of the target file through the file pointer.
fscanf(): The fscanf() function works like the scanf() function. The difference is that fscanf() reads from a file and not from the computer keyboard.
Fprintf and fscanf example:
A warning may appear on your compiler, but it will work!
The C function library int fputs (
const char * str, FILE * stream) writes a string to the specified stream up to, but not including, the null character.
Following is the statement for the fputs() function:
int fputs (const char * str, FILE * stream)
- str - This is an array containing the null terminating string of characters to be written.
- stream - This is the pointer to a file object that identifies the stream where the string is to be written.
Writes a string to a file. This function returns a non-negative value plus, in case of error returns
Then run at the terminal (even if you didn’t create the file before):
cat my_file.txt and look at the content created with your C code! =)
To read a string in a file we can use fgets() whose prototype is:
char *fgets (char *str, int the_size,FILE *fp);
The function takes 3 arguments: the string to read, the upper limit of characters to read, and the pointer to FILE, which is associated with the file from which the string will be read. The function reads the string until a newline character. either read or length-1 characters have been read. If the newline character (‘
\n’) is read, it will be part of the string, which was not the case with gets. The resulting string will always end with ‘
\0’ (so only 1 character length maximum will be read). The fgets function is similar to the
gets() function, but in addition to being able to read from a data file and including the newline character in the string, it still specifies the maximum length of the input string. As we have seen, the gets function did not have this control, which could lead to “buffer overflow” errors. Therefore, given that the fp pointer can be replaced by stdin, as we saw above, an alternative to using gets is to use the following construct:
fgets (str, the_size, stdin);
where str is the string being read and size must be equal to the size allocated for the string subtracted from 1, because of the
int ferror (FILE *fp);. The function returns zero if no errors occurred and a nonzero number if any errors occurred while accessing the file.
ferror()becomes very useful when we want to verify that each access to a file has been successful so that we can ensure the integrity of our data. In most cases, if a file can be opened, it can be read or written. However, there are situations where this does not occur. For example, you may run out of disk space while recording, or the disk may be bad and unable to read, etc. One function that can be used in conjunction with
perror()(print error) function, whose argument is a string that usually indicates where in the program the problem occurred.
To make random searches and accesses in files use the function
fseek(). It moves the current read or write position in the file to a specified value from a specified point. Your prototype is:
int fseek (FILE *fp,long numbytes,int origin );
The source parameter determines where the move numbytes will be counted from. Possible values are defined by macros in stdio.h
The prototype rewind() function:
void rewind (FILE *fp);
returns the current file position to the beginning.