C Program to Change Nth Character in a Text File

Introduction

In this post, I am going to write a c program to change the nth character in a text file. Simply, we have to read a text file, ask the user to enter the character and location to replace or change the character.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Let us see the following expected output.

Input File Before Changing Character

Hello, this is hplus academy. This program is going to change the nth character with the new character.
c program to change nth character in a text file output

Input File After Changing Character

Hello, this is Hplus academy. This program is going to change the nth character with the new character.

As you can see the above input file is “abc.txt”. We have replaced small h with the capital H. For this, we have asked the user to enter a character and the location.

Also Read: C Program to Print Multiples of 5 using do-while loop

This is a very simple program. Now, let us see the actual program and after that, I will explain some important parts of this program.

C Program to Change Nth Character in a Text File

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char ch;
    int loc;
    fp=fopen("abc.txt","r+");
    printf("\nEnter a character: ");
    scanf("%c",&ch);
    printf("Enter the location of the character: ");
    scanf("%d",&loc);
    fseek(fp,loc-1,SEEK_SET);
    putc(ch,fp);
    fclose(fp);
    return 0;
}

This is the c program that is going to change the nth character with another character. Now, I am going to explain the program.

Variable Declarations

In this program, I have declared three variables namely fp, ch and loc.

The variable fp is a file pointer of type FILE. Using this file pointer, we will open a file for writing.

When the user enters the character, then we will store that character in the variable ch of type char.

We also need a location to change the character. For this, we have declared the variable loc of type int.

fp=fopen(“abc.txt”,”r+”);

This statement will open a file abc.txt in writing mode. But here, I have used ‘r+’ instead of ‘w’. When we use w, then all the existing contents of the file will be deleted. But, we need to edit the existing file. For this, we have the file mode ‘r+’.

fseek(fp,loc-1,SEEK_SET);

This is a very important file handling function. This function sets the file pointer at the desired location in a file.

In the above function, it has taken three parameters. First is fp, second is offset and third is position.

The fp is a file pointer that identifies the file.

Here, the offset is loc-1. That means the file pointer moves from loc-1 bytes from the beginning.

The SEEK_SET means from the beginning.

So fseek() will move file pointer from beginning loc-1 bytes.

putc(ch,fp);

We have already set the file pointer at the desired location. This function will write a single character to the file.

I hope you have understood this program. If you have any difficulties, please contact me.

Thank you.

Some Important Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment