The Wayback Machine - https://web.archive.org/web/20241109134354/https://www.geeksforgeeks.org/c-file-pointer/
Open In App

C File Pointer

Last Updated : 06 Jun, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A file pointer is a variable that is used to refer to an opened file in a C program. The file pointer is actually a structure that stores the file data such as the file name, its location, mode, and the current position in the file. It is used in almost all the file operations in C such as opening, closing, reading, writing, etc.

Syntax

FILE *ptr;

Here, FILE is the typedef name of the predefined file pointer structure and ptr is a pointer variable of type FILE.

Example of File Pointer

C




// C Program to demonstrate the file pointer
#include <stdio.h>
  
int main()
{
    // declaring file pointer
    FILE* fptr;
  
    // trying to get the size of FILE datatype.
    printf("Size of FILE Structure: %d bytes",
           sizeof(FILE));
  
    return 0;
}


Output

Size of FILE Structure: 216 bytes

How File Pointer Works in C?

We use a file pointer to refer to the file opened using fopen() function and the behavior of a file pointer can vary depending on the access modes specified when opening the file using the fopen() function.

Let’s see how the C file pointer works in files with different access modes:

1. In Read Mode ( “r” )

Syntax

FILE *fp;
fp = fopen("fileName", "r");
  • The position of the file pointer is initially at the beginning of the file.
  • When we read data from a file using functions like fgetc(), fgets(), etc., the file pointer moves forward automatically to the next position after the read operation.
  • We cannot perform write operations using file pointer referring to the file opened in read mode.

2. In Write Mode ( “w” )

Syntax

FILE *fp;
fp = fopen("fileName", "w");
  • If the file exists, the position of the file pointer is initially at the beginning of the file.
  • The existing content in the file is overwritten when we write data to the file.
  • When we write data to the file using functions like fputc(), fprintf(), etc., the file pointer moves forward automatically to the next position after the write operation.
  • Read operations like fgetc() or fgets() cannot be performed on file pointer pointing to these files.

3. Append Mode ( “a” )

Syntax

FILE *fp;
fp = fopen("fileName", "a");
  • In append mode, the file pointer is positioned at the end of the file.
  • The file pointer automatically moves forward to the next position after each write operation.

How File Pointer Works in fseek() function?

fseek() function in C is used to set or change the file pointer to a specific position within a file.

Syntax

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

Parameters

  • filePointer: The file pointer we want to modify.
  • offset: The number of bytes to move the file pointer that can be positive or negative.
  • origin: The starting point from where the offset is calculated. It can take one of the following values:
    • SEEK_SET: Beginning of the file.
    • SEEK_CUR: Current position of the file pointer.
    • SEEK_END: End of the file.

When we call fseek() and provide the value of offset and origin, the position of the file pointer is changed accordingly. The new position is determined by adding the offset to the origin. It returns 0 if the operation is successful and it returns a non-zero value if an error occurred.

Conclusion

File pointers in C are important for performing input and output operations on files. They work as an interface between the program and the file, allowing us to read from and write to the file.



Similar Reads

C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
5 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction Consider the following program: [GFGTABS] C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf("%p\n", ptr); return 0; } [/GFGTABS]In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer th
9 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the & operator array is an alias for &array[0
1 min read
C | Pointer Basics | Question 1
What is the output of following program? # include <stdio.h> void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf("%d", y); return 0; } (A) 30 (B) 20 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using
1 min read
C | Pointer Basics | Question 2
Output of following program? # include <stdio.h> void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&y); printf("%d", y); return 0; } (A) 20 (B) 30 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value
1 min read
C | Pointer Basics | Question 3
Output of following program? #include <stdio.h> int main() { int *ptr; int x; ptr = &x; *ptr = 0; printf(" x = %d\n", x); printf(" *ptr = %d\n", *ptr); *ptr += 5; printf(" x = %d\n", x); printf(" *ptr = %d\n", *ptr); (*ptr)++; printf(" x = %d\n", x); printf(" *ptr = %d\n", *ptr);
2 min read
C | Pointer Basics | Question 4
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include <stdio.h> int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf("sizeof arri[] = %d ", sizeof(arri)); printf("sizeof ptri = %d ", sizeof(ptri)); printf("sizeof arrc[] =
1 min read
C | Pointer Basics | Question 17
Assume that float takes 4 bytes, predict the output of following program. #include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f ", *ptr2); printf("%d", ptr2 - ptr1); return 0; } (A) 90.500000 3 (B) 90.500000 12 (C) 10.000000 12 (D) 0.5000
1 min read
C | Pointer Basics | Question 6
#include<stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf("Number of elements between two pointer are: %d.", (ptr2 - ptr1)); printf("Number of bytes between two pointers are: %d", (char*)ptr2 - (char*) ptr1); return 0; } Assume that an int variable takes 4 bytes and a
2 min read
C | Pointer Basics | Question 7
#include<stdio.h> int main() { int a; char *x; x = (char *) &a; a = 512; x[0] = 1; x[1] = 2; printf("%d\n",a); return 0; } What is the output of above program? (A) Machine dependent (B) 513 (C) 258 (D) Compiler Error Answer: (A) Explanation: Output is 513 in a little endian machine. To understand this output, let integers be sto
1 min read
C | Pointer Basics | Question 8
C/C++ Code int main() { char *ptr = \"GeeksQuiz\"; printf(\"%c", *&*&*ptr); return 0; } (A) Compiler Error (B) Garbage Value (C) Runtime Error (D) G Answer: (D)Explanation: The operator * is used for dereferencing and the operator & is used to get the address. These operators cancel out effect of each other when used one after another. We can a
1 min read
C | Advanced Pointer | Question 1
C/C++ Code void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf(\"%d\", *p); return 0; } (A) 10 (B) 20 (C) Compiler error (D) Runtime Error Answer: (B)Explanation: Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains uneffected. If we want to change a local poi
2 min read
C | Advanced Pointer | Question 2
Assume sizeof an integer and a pointer is 4 byte. Output? #include <stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf("%d", sizeof(*p)); getchar(); return 0; } (A) 200 (B) 4 (C) 800 (D) 80 Answer: (C) Explanation: Output is 10*20*sizeof(int) which is “800″ for compilers with integer size as 4 bytes. When a point
1 min read
C | Pointer Basics | Question 17
#include<stdio.h> void fun(int arr[]) { int i; int arr_size = sizeof(arr)/sizeof(arr[0]); for (i = 0; i < arr_size; i++) printf("%d ", arr[i]); } int main() { int i; int arr[4] = {10, 20 ,30, 40}; fun(arr); return 0; } (A) 10 20 30 40 (B) Machine Dependent (C) 10 20 (D) Northing Answer: (B) Explanation: In C, array parameters are
2 min read
C | Pointer Basics | Question 10
The reason for using pointers in a C program is (A) Pointers allow different functions to share and modify their local variables. (B) To pass large structures so that complete copy of the structure can be avoided. (C) Pointers enable complex “linked\" data structures like linked lists and binary trees. (D) All of the above Answer: (D)Explanation: S
1 min read
C | Pointer Basics | Question 11
C/C++ Code #include void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&i;, &j;); printf(\"%d %d", i, j); getchar(); return 0; } (A) 2 2 (B) 2 1 (C) 0 1 (D) 0 2 Answer: (D)Explanation: See below f() with comments for explanation. /* p points to i and q points to j */ void f(int *p, int *q) { p = q; /* p also points to j now */
1 min read
C | Pointer Basics | Question 12
Consider this C code to swap two integers and these five statements after it: void swap(int *px, int *py) { *px = *px - *py; *py = *px + *py; *px = *py - *px; } S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers r
1 min read
C | Pointer Basics | Question 13
int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } void main() { int c, *b, **a; c = 4; b = &c; a = &b; printf("%d ", f(c, b, a)); return 0; } (A) 18 (B) 19 (C) 21 (D) 22 Answer: (B) Explanation: Let us understand this line by line /* below line changes value of c to
1 min read
C | Pointer Basics | Question 14
Predict the output of following program #include<stdio.h> int main() { int a = 12; void *ptr = (int *)&a; printf("%d", *ptr); getchar(); return 0; } (A) 12 (B) Compiler Error (C) Runt Time Error (D) 0 Answer: (B) Explanation: There is compiler error in line "printf("%d", *ptr);". void * type pointers cannot be de-referenced. We
1 min read
C | Advanced Pointer | Question 3
#include <stdio.h> int main() { int a[5] = {1,2,3,4,5}; int *ptr = (int*)(&a+1); printf("%d %d", *(a+1), *(ptr-1)); return 0; } (A) 2 5 (B) Garbage Value (C) Compiler Error (D) Segmentation Fault Answer: (A) Explanation: The program prints “2 5″. Since compilers convert array operations in pointers before accessing the array ele
1 min read
C | Advanced Pointer | Question 4
#include <stdio.h> char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"}; char **cp[] = {c+3, c+2, c+1, c}; char ***cpp = cp; int main() { printf("%s ", **++cpp); printf("%s ", *--*++cpp+3); printf("%s ", *cpp[-2]+3); printf("%s ", cpp[-1][-1]+1); return 0; } (A) TEST s
1 min read
C | Pointer Basics | Question 15
#include<stdio.h> void swap (char *x, char *y) { char *t = x; x = y; y = t; } int main() { char *x = "geeksquiz"; char *y = "geeksforgeeks"; char *t; swap(x, y); printf("(%s, %s)", x, y); t = x; x = y; y = t; printf("\n(%s, %s)", x, y); return 0; } (A) (geeksquiz, geeksforgeeks) (geeksforgeeks, geeksqui
1 min read
C | Pointer Basics | Question 16
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; ++*p; p += 2; printf("%d", *p); return 0; } (A) 2 (B) 3 (C) 4 (D) Compiler Error Answer: (B) Explanation: The expression ++*p is evaluated as "++(*p)" . So it increments the value of first element of array (doesn't change the pointer p). When p += 2 is done,
1 min read
C | Advanced Pointer | Question 5
Predict the output #include <string.h> #include <stdio.h> #include <stdlib.h> void fun(char** str_ref) { str_ref++; } int main() { char *str = (void *)malloc(100*sizeof(char)); strcpy(str, "GeeksQuiz"); fun(&str); puts(str); free(str); return 0; } (A) GeeksQuiz (B) eeksQuiz (C) Garbage Value (D) Compiler Error Answer
1 min read
C | Advanced Pointer | Question 7
Assume that the size of int is 4. #include <stdio.h> void f(char**); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char **p) { char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t); } (A) ab (B) cd (C) ef (D) gh Answer: (D)
1 min read
C | Advanced Pointer | Question 8
C/C++ Code (A) 2 3 5 6 (B) 2 3 4 5 (C) 4 5 0 0 (D) none of the above Answer: (A) Explanation: In the above code, we have declared: int a[][3] = {1, 2, 3, 4, 5, 6}; This line declares a 2D array with 2 rows and 3 columns. The elements are initialized in row-major order: 1, 2, and 3 are the elements of the first row, and 4, 5, and 6 are the elements
2 min read
C | Advanced Pointer | Question 9
#include <stdio.h> #include <stdlib.h> int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0; i<5; i++) *(ptr + i) = i; printf("%d ", *ptr++); printf("%d ", (*ptr)++); printf("%d ", *ptr); printf("%d ", *++ptr); printf("%d ", ++*ptr); } (A) Compiler Error (B) 0
1 min read
C | Advanced Pointer | Question 10
Output of following program #include <stdio.h> int fun(int arr[]) { arr = arr+1; printf("%d ", arr[0]); } int main(void) { int arr[2] = {10, 20}; fun(arr); printf("%d", arr[0]); return 0; } (A) Compiler Error (B) 20 10 (C) 20 20 (D) 10 10 Answer: (B) Explanation: In C, array parameters are treated as pointers (See https://
1 min read
Article Tags :
three90RightbarBannerImg