C Program to Replace a Specific Line in a Text File

This C Program replace a specified line in a text file.

Here is source code of the C Program to replace a specified line in a text file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Replace a specified Line in a Text File
  3.  */
  4. #include <stdio.h>
  5.  
  6. int main(void)
  7. {
  8.     FILE *fileptr1, *fileptr2;
  9.     char filechar[40];
  10.     char c;
  11.     int delete_line, temp = 1;
  12.  
  13.     printf("Enter file name: ");
  14.     scanf("%s", filechar);
  15.     fileptr1 = fopen(filechar, "r");
  16.     c = getc(fileptr1);
  17.     //print the contents of file .
  18.     while (c != EOF)
  19.     {
  20.         printf("%c", c);
  21.         c = getc(fileptr1);
  22.     }
  23.     printf(" \n Enter line number to be deleted and replaced");
  24.     scanf("%d", &delete_line);
  25.     //take fileptr1 to start point.
  26.     rewind(fileptr1);
  27.     //open replica.c in write mode
  28.     fileptr2 = fopen("replica.c", "w");
  29.     c = getc(fileptr1);
  30.     while (c != EOF)
  31.     {
  32.         if (c == 'n')
  33.         {
  34.             temp++;
  35.         }
  36.         //till the line to be deleted comes,copy the content to other
  37.         if (temp != delete_line)
  38.         {
  39.             putc(c, fileptr2);
  40.         }
  41.         else
  42.         {
  43.             while ((c = getc(fileptr1)) != 'n')
  44.             {
  45.             }
  46.             //read and skip the line ask for new text
  47.             printf("Enter new text");
  48.             //flush the input stream
  49.             fflush(stdin);
  50.             putc('n', fileptr2);
  51.             //put 'n' in new file
  52.             while ((c = getchar()) != 'n')
  53.                 putc(c, fileptr2);
  54.             //take the data from user and place it in new file
  55.             fputs("n", fileptr2);
  56.             temp++;
  57.         }
  58.         //continue this till EOF is encountered
  59.         c = getc(fileptr1);
  60.     }
  61.     fclose(fileptr1);
  62.     fclose(fileptr2);
  63.     remove(filechar);
  64.     rename("replica.c", filechar);
  65.     fileptr1 = fopen(filechar, "r");
  66.     //reads the character from file
  67.     c = getc(fileptr1);
  68.     //until last character of file is encountered
  69.     while (c != EOF)
  70.     {
  71.         printf("%c", c);
  72.         //all characters are printed
  73.         c = getc(fileptr1);
  74.     }
  75.     fclose(fileptr1);
  76.     return 0;
  77. }

$ cc pgm48.c
$ a.out
Enter file name: pgm3.c
/*
 * C Program to Convert Octal to Decimal
 */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
 
    long int octal, decimal = 0;
    int i = 0;
 
    printf("Enter any octal number: ");
    scanf("%ld", &octal);
    while (octal != 0)
    {
        decimal =  decimal +(octal % 10)* pow(8, i++);
        octal = octal / 10;
    }
    printf("Equivalent decimal value: %ld",decimal);
    return 0;
}
 
Enter line number to be deleted and replaced 13 replaced
Enter new text
/*
 * C Program to Convert Octal to Decimal
 */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
 
    long int octal, decimal = 0;
    int i = 0;
 replaced
    printf("Enter any octal number: ");
    scanf("%ld", &octal);
    while (octal != 0)
    {
        decimal =  decimal +(octal % 10)* pow(8, i++);
        octal = octal / 10;
    }
    printf("Equivalent decimal value: %ld",decimal);
    return 0;
}

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on File Handling, go to File Handling. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.