The C programming language provides the fread()
method which is used to read files and store the data in the buffer. The buffer is a memory area in order to store temporary data. In this tutorial, we examine how to use the fread() method in different ways.
fread() Method Syntax
The fread() method has the following syntax.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
- size_t is the fread() method readed size which is the return value of the fread() method.
- void *ptr is the buffer where read data will be stored. This parameter is requires.
- size_t size is the size of single element generally specified with character types by using the sizeof operator. This option is required.
- size_t nmemb is the number of the items which will be read from the file.
- FILE *stream is the file descriptor which will be read.
Read Specified Size of Characters
In order to read a file into buffer the file should be opened with the fopen()
method. There are different modes to open a file like read, write, read-only, write-only, append, etc. As you expect the file should be opened with the read-related mode which can be also read+write. In the following example we will read 5 times 2 byte.
#include <stdio.h>
#include <string.h>
int main () {
FILE *fp;
char buffer[100];
/* Open file for both reading and writing */
fp = fopen("file.txt", "r");
fread(buffer, 5, 2, fp);
printf("%s\n", buffer);
fclose(fp);
return(0);
}
Read Byte by Byte
The fread() method can be used to read byte by byte where the size is provided as one byte and the number of read count is provided. In the following example we read 20 times 1 byte which is in total 20 bytes.
#include <stdio.h>
#include <string.h>
int main () {
FILE *fp;
char buffer[100];
/* Open file for both reading and writing */
fp = fopen("file.txt", "r");
fread(buffer, 20, 1, fp);
printf("%s\n", buffer);
fclose(fp);
return(0);
}
Specify Read Size
The byte count for every read can be specified as numbers like 1 byte, 2 bytes etc. But also C structures can be used to specify the size which is less errorprone and more dynamic. For example, we can specify the integer size can be specified by using the sizeof(int)
like below.
#include <stdio.h>
#include <string.h>
int main () {
FILE *fp;
char buffer[100];
/* Open file for both reading and writing */
fp = fopen("file.txt", "r");
fread(buffer, 20, sizeof(int), fp);
printf("%s\n", buffer);
fclose(fp);
return(0);
}
Navigate In File and Then Read
The fread() method starts reading a file from the begging of the file and continues in every read from there it is left. But in some cases read straight is not the option. The fseek()
method can be used to navigate to the character count we want to start reading. In the following example, we navigate to the 20th character and then start reading with the fread() method.
#include <stdio.h>
#include <string.h>
int main () {
FILE *fp;
char buffer[100];
/* Open file for both reading and writing */
fp = fopen("file.txt", "r");
fseek(fp, 20, SEEK_SET);
fread(buffer, 20, sizeof(int), fp);
printf("%s\n", buffer);
fclose(fp);
return(0);
}