File Pointer in C
Introduction
The C programming language depends on file input and output procedures. Clients may save and access data using files, a non-volatile memory system. For carrying out file I/O operations, programmers in C are given access to various methods. The file pointer in C is among the crucial aspects of file I/O.
What is File Pointer in C?
In C, a data type that points to a file is known as a file pointer. It is a structure that stores details about files, including their names, where they are stored, and how they are accessible. A file pointer is employed to regulate its location within the file as well as read from and write to files. The program can interact with the file in various ways due to the interface it serves as between them.
All things considered, file pointers are a crucial part of C language since they allow programs to read and write data from files, which is a prerequisite for several applications.
The syntax for File Pointer in C
A file pointer can be declared in C using the following syntax:
FILE *filePointer;
In this case, handling file I/O operations is done using the pre-defined C structure, FILE. The memory address of the file's present location will be kept in a variable called file pointer.
Functions that use File Pointer in C
Several functions in C that handle file input and output employ the file pointer. Following are a few examples of frequently encountered file pointer functions:
1. fopen(): A pointer to the opened file is returned by the fopen() function, which is used to open a file.
Let us see an example for fopen() using a file pointer
Code
#include <stdio.h>
int main() {
FILE *fp; // declaring file pointer
fp = fopen("Example.txt", "r");
return 0;
}
In the above example, the fopen() function opens a file named Example.txt in read mode.
2. fclose(): A file is closed using the fclose() method. The file pointer is passed to it as a parameter.
Here is an example code for fclose() using a file pointer
#include <stdio.h>
int main() {
FILE *fp; // declaring file pointer
fp = fopen("Example.txt", "r");
fclose(fp);
return 0;
}
The fclose() function is used in the example above to close the Example.txt file, which was opened earlier with the fopen() method.
3. fseek(): A particular location in the file can be reached by using the fseek() method to relocate the file pointer.
Syntax for fseek()
fseek(FILE *filePointer, long offset, int origin);
It requires the following three factors:
- file pointer: A pointer to the file you want to edit.
- offset A long integer value indicating how many bytes should be added to the file pointer's distance from the origin. A positive value advances the file pointer, whereas a negative value advances it backward.
- origin: A constant in the form of an integer that indicates where the file pointer movement should begin. One of the following possibilities exists:
- SEEK_SET: Beginning of the file.
- SEEK_CUR: Current position of the file pointer.
- SEEK_END: End of the file.
Let us look at an example code of fseek() using file pointer
#include <stdio.h>
int main() {
FILE *fp; // declaring the file pointer
int c;
fp = fopen("Example.txt", "r");
fseek(fp, 0, SEEK_SET);
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
return 0;
}
The fseek() function is used to advance the file pointer to the beginning of a file in the example above.
4. ftell(): The present location of the file pointer is returned by the ftell() function.
Let us have an example for ftell() using file pointer
#include <stdio.h>
int main() {
FILE *fp; // declaring file pointer
int pos;
fp = fopen("Example.txt", "r");
pos = ftell(fp); // pos contains the current position
fclose(fp);
return 0;
}
The ftell() function is used in the example above to determine the file pointer's current location.
5. read (): Data is read from a file using the read () method.
Syntax for fread()
size_t fread(void *ptr, size_t size, size_t count, FILE *filePointer);
The following are the parameters for the fread() function:
- ptr: A pointer to the buffer, which will store the file's read contents.
- size: The size of data to be read, expressed in bytes.
- count The number of items that will be read from the file.
- file pointer: A reference to the file that will be read from.
Here is an example of fread() using file pointer
#include <stdio.h>
int main() {
FILE *fp; // declaring file pointer
char str[100];
fp = fopen("Example.txt", "r");
fread(str, sizeof(char), 100, fp);
fclose(fp);
return 0;
}
The read () function is used to read a string from a file in the example above.
6. write (): Data is written to a file using the write () method.
Syntax for fwrite()
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *filePointer);
The following are the parameters of the fwrite() function:
- ptr: A pointer to the buffer containing the data that will be written to the file.
- size: The number of bytes that make up each item that needs to be written.
- count The number of items that will be written to the file.
- file pointer: A reference to the file that will be written to.
Example of fwrite() using filepointer
#include <stdio.h>
int main()
{
FILE *filePointer; // declaring a file pointer
char str[] = "Hello!";
filePointer = fopen("test.txt", "w");
fwrite(str, 1, sizeof(str), filePointer);
fclose(filePointer);
return 0;
}
Conclusion
With the help of a file pointer, we may read from or write to a file in C while navigating through it. The file's present location is indicated by what is effectively a pointer. For example, the methods fopen(), fclose(), fseek(), ftell(), freed (), write (), etc., all employ file pointers when performing input/output operations on files.