C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - File Handling - Randomly Access Files

C Programming

In this tutorial we will learn to randomly access file data in C programming language.

In this tutorial we will be discussing the fseek(), ftell() and rewind() functions to access data in a file.

The ftell function

The ftell() function tells us about the current position in the file (in bytes).

Syntax:

pos = ftell(fptr);

Where, fptr is a file pointer. pos holds the current position i.e., total bytes read (or written).

Example:

If a file has 10 bytes of data and if the ftell() function returns 4 then, it means that 4 bytes has already been read (or written).

The rewind function

We use the rewind() function to return back to the starting point in the file.

Syntax:

rewind(fptr);

Where, fptr is a file pointer.

The fseek function

We use the fseek() function to move the file position to a desired location.

Syntax:

fseek(fptr, offset, position);

Where, fptr is the file pointer. offset which is of type long, specifies the number of positions (in bytes) to move in the file from the location specified by the position.

The position can take the following values.

  • 0 - The beginning of the file
  • 1 - The current position in the file
  • 2 - End of the file

Following are the list of operations we can perform using the fseek() function.

OperationDescription
fseek(fptr, 0, 0)This will take us to the beginning of the file.
fseek(fptr, 0, 2)This will take us to the end of the file.
fseek(fptr, N, 0)This will take us to (N + 1)th bytes in the file.
fseek(fptr, N, 1)This will take us N bytes forward from the current position in the file.
fseek(fptr, -N, 1)This will take us N bytes backward from the current position in the file.
fseek(fptr, -N, 2)This will take us N bytes backward from the end position in the file.

Write a program in C to save alphabet A to Z in file and then print the letters using fseek function

#include <stdio.h>

int main(void) {
    
  // integer variable
  int i;
    
  // character variable
  char ch;
  
  // file pointer
  FILE *fptr;
  
  // open file in write mode
  fptr = fopen("char", "w");
  
  if (fptr != NULL) {
    printf("File created successfully!\n");
  }
  else {
    printf("Failed to create the file.\n");
    // exit status for OS that an error occured
    return -1;
  }
  
  // write data in file
  for (ch = 'A'; ch <= 'Z'; ch++) {
    putc(ch, fptr);
  }
  
  // close connection
  fclose(fptr);
  
  // reference
  printf("\nReference:\n");
  for (i = 0; i < 26; i++) {
    printf("%d ", (i+1));
  }
  printf("\n");
  for (i = 65; i <= 90; i++) {
    // print character
    printf("%c", (i));
    
    // manage space
    if (i - 65 >= 9) {
      printf("  ");
    }
    else {
      printf(" ");
    }
  }
  printf("\n\n");
  
  
  // open file for reading
  fptr = fopen("char", "r");
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 1st char in the file
  fseek(fptr, 0, 0);
  ch = getc(fptr);
  printf("1st char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 5th char in the file
  fseek(fptr, 4, 0);
  ch = getc(fptr);
  printf("5th char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 26th char in the file
  fseek(fptr, 25, 0);
  ch = getc(fptr);
  printf("26th char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // rewind
  printf("rewind\n");
  rewind(fptr);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 10th char in the file
  fseek(fptr, 9, 0);
  ch = getc(fptr);
  printf("10th char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 15th char in the file
  fseek(fptr, 4, 1);    // move 4 bytes forward from current position
  ch = getc(fptr);
  printf("15th char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // read 20th char in the file
  fseek(fptr, 4, 1);    // move 4 bytes forward from current position
  ch = getc(fptr);
  printf("20th char: %c\n", ch);
  
  printf("Curr pos: %ld\n", ftell(fptr));
  
  // close connection
  fclose(fptr);
  
  return 0;
}

Output:

File created successfully!

Reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
A B C D E F G H I J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  

Curr pos: 0
1st char: A
Curr pos: 1
5th char: E
Curr pos: 5
26th char: Z
Curr pos: 26
rewind
Curr pos: 0
10th char: J
Curr pos: 10
15th char: O
Curr pos: 15
20th char: T
Curr pos: 20