Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

File Pointer in C

Last Updated on August 9, 2023 by Mayank Dham

File input/output operations are essential in the C programming language. Files are a type of non-volatile storage medium that allows users to store and retrieve data. In C, users are provided with a number of functions for performing file I/O operations. One of the important features of file I/O is the file pointer in C. This article will explain what is file pointer in C, and what functions in C use file pointer.

What is File Pointer in C?

A file pointer in C is a data type that is used to point to a file. It is a structure that holds information such as the name of the file, its location, and the mode in which the file is accessed.

A file pointer is used to read from and write to files, as well as to control its position inside the file. It acts as an interface between the program and the file, allowing the program to interact with the file in a variety of ways.

Overall we can say, file pointers are an important aspect of C programming because they enable programs to read and write data from files, which is a requirement for many applications.

Syntax of File Pointer in C

The syntax used for declaring a file pointer in C is as follows:

FILE *filePointer;

Here, FILE is a pre-defined structure in C that is used to handle file I/O operations. The filePointer is a variable that will store the memory address of the file’s current position.

Functions that use File Pointer in C

The file pointer in C is used in various functions that perform file input/output operations. Some of the commonly used functions in which file pointer is used are

  1. fopen(): The fopen() function is used to open a file and returns a pointer to the opened file.

    Here is an example:

    #include <stdio.h>
    
    int main() {
       FILE *fp; // declaring file pointer
    
       fp = fopen("PrepBytes.txt", "r");
       return 0;
    }

    In the above example we use the fopen() function to open a file named PrepBytes.txt in read mode.

  2. fclose(): The fclose() function is used to close a file. It receives the file pointer as a parameter.

    Here is an example:

    #include <stdio.h>
    
    int main() {
       FILE *fp; // declaring file pointer
    
       fp = fopen("PrepBytes.txt", "r");
       fclose(fp);
       return 0;
    }

    In the above example we use the fclose() function to close PrepBytes.txt file that was previously opened with the fopen() function.

  3. fseek(): The fseek() function is used to move the file pointer to a specific position in the file.

    Syntax of fseek()

    fseek(FILE *filePointer, long offset, int origin);

    It takes three parameters:

    • filePointer: a pointer to the file you want to manipulate.
    • offset: a long integer value that specifies the number of bytes to move the file pointer relative to the position specified by origin. A positive value moves the file pointer forward, while a negative value moves it backward.
    • origin: an integer constant that specifies the starting position for the file pointer movement. It can be one of the following:

      • SEEK_SET: beginning of the file.
      • SEEK_CUR: current position of the file pointer.
      • SEEK_END: end of the file.

    Here is an example:

    #include <stdio.h>
    
    int main() {
       FILE *fp; // declaring the file pointer
       int c;
    
       fp = fopen("PrepBytes.txt", "r");
       fseek(fp, 0, SEEK_SET);
       while ((c = getc(fp)) != EOF) {
          putchar(c);
       }
       fclose(fp);
       return 0;
    }

    In the above example we use the fseek() function to move the file pointer to the beginning of a file.

  4. ftell(): The ftell() function is used to return the current position of the file pointer.

    Here is an example:

    #include <stdio.h>
    
    int main() {
       FILE *fp; // declaring file pointer
       int pos;
    
       fp = fopen("PrepBytes.txt", "r");
       pos = ftell(fp); // pos contains the current position
       fclose(fp);
       return 0;
    }

    In the above example we use the ftell() function to get the current position of the file pointer.

  5. fread(): The fread() function reads data from a file.

    Syntax of fread()

    size_t fread(void *ptr, size_t size, size_t count, FILE *filePointer);

    The parameters of the fread() function are as follows:

    • ptr: A pointer to the buffer where the data read from the file will be stored.
    • size: The size of each item to be read, in bytes.
    • count: The number of items to be read from the file.
    • filePointer: A pointer to the file to be read from.

    Here is an example:

    #include <stdio.h>
    
    int main() {
       FILE *fp; // declaring file pointer
       char str[100];
    
       fp = fopen("PrepBytes.txt", "r");
       fread(str, sizeof(char), 100, fp);
       fclose(fp);
       return 0;
    }

    In the above example we use the fread() function to read a string from a file.

  6. fwrite(): The fwrite() function is used to write data to a file.

    Syntax of fwrite()

    size_t fwrite(const void *ptr, size_t size, size_t count, FILE *filePointer);

    The parameters of the fwrite() function are as follows:

    • ptr: A pointer to the buffer containing the data to be written to the file.
    • size: The size of each item to be written, in bytes.
    • count: The number of items to be written to the file.
    • filePointer: A pointer to the file to be written to.

    Here is an example:

    #include <stdio.h>
    
    int main()
    {
       FILE *filePointer; // declaring a file pointer
    
       char str[] = "Hello PrepBytes!";
       filePointer = fopen("test.txt", "w");
       fwrite(str, 1, sizeof(str), filePointer);
       fclose(filePointer);
       return 0;
    }

    In the above example we use the fwrite() function to write “Hello PrepBytes!” to a file.

Conclusion
A file pointer in C is a mechanism that allows us to navigate through a file while reading from or writing to it. It is essentially a pointer that points to the current position of the file. File pointers are used in various functions that perform file input/output operations, such as fopen(), fclose(), fseek(), ftell(), fread(), fwrite(), etc.

FAQs

Here are some frequently asked questions about file pointer in C.

Q1: What is the use of file pointer in C?
Ans: File pointers are used to navigate through a file while reading from or writing to it, seek a specific position in the file, and check the end of the file.

Q2: How do I read a binary file in C?
Ans: To read a binary file in C, use the fread() function. You need to open the file in binary mode using fopen() with "rb" mode, and then use fread() to read data from the file. You can use the sizeof() operator to determine the size of the data to be read from the file.

Q3: How do I write to a file in C?
Ans: To write to a file in C, use the fwrite() function. You need to open the file in write mode using fopen() with "w" or "wb" mode, and then use fwrite() to write data to the file. You can use the sizeof() operator to determine the size of the data to be written to the file.

Q4: Can I use a file pointer to read from multiple files simultaneously?
Ans: Yes, you can use a file pointer to read from multiple files simultaneously by opening each file with a unique file pointer. You can then use these file pointers to read data from each file simultaneously.

5. How to move the file pointer position within a file?
The fseek() function is used to move the file pointer to a specific position within the file. It takes three arguments: the file pointer, the offset from a specified origin, and the origin itself. Common origins include SEEK_SET (start of file), SEEK_CUR (current position), and SEEK_END (end of file). For example:

fseek(filePtr, 10, SEEK_SET); // Moves the pointer 10 positions from the start of the file

Leave a Reply

Your email address will not be published. Required fields are marked *