banner



How To Change One Specific Line Of A File C++

C Exercises: Supervene upon a specific line in a file with a new text

C File Treatment : Exercise-ix with Solution

Write a plan in C to supersede a specific line with another text in a file.

Presume that the content of the file test.txt is :                                                                        test line 1                                                                                                    examination line two                                                                                                    test line 3                                                                                                    examination line four          

Sample Solution:

C Code:

            #include <stdio.h> #include <string.h>  #define MAX 256    int main()    {         FILE *fptr1, *fptr2;         int lno, linectr = 0;         char str[MAX],fname[MAX];                 char newln[MAX], temp[] = "temp.txt"; 		 		printf("\north\north Replace a specific line in a text file with a new text :\n"); 		printf("-------------------------------------------------------------\n");  		printf(" Input the file name to be opened : ");         fgets(fname, MAX, stdin);         fname[strlen(fname) - 1] = '\0';         fptr1 = fopen(fname, "r");         if (!fptr1)          {                 printf("Unable to open up the input file!!\northward");                 return 0;         }         fptr2 = fopen(temp, "w");         if (!fptr2)          {                 printf("Unable to open a temporary file to write!!\due north");                 fclose(fptr1);                 return 0;         }         /* get the new line from the user */         printf(" Input the content of the new line : ");         fgets(newln, MAX, stdin);         /* go the line number to delete the specific line */         printf(" Input the line no you want to supervene upon : ");         scanf("%d", &lno);         lno++;          // copy all contents to the temporary file other except specific line         while (!feof(fptr1))          {             strcpy(str, "\0");             fgets(str, MAX, fptr1);             if (!feof(fptr1))              {                 linectr++;                 if (linectr != lno)                      {                         fprintf(fptr2, "%s", str);                     }                      else                      {                         fprintf(fptr2, "%southward", newln);                     }                 }         }         fclose(fptr1);         fclose(fptr2);         remove(fname);         rename(temp, fname);         printf(" Replacement did successfully..!! \northward");         return 0;   }                      

Sample Output:

Replace a specific line in a text file with a new text :                                                      -------------------------------------------------------------                                                   Input the file proper name to be opened : test.txt                                                                    Input the content of the new line : 2                                                                          Input the line no you lot desire to replace : two                                                                      Replacement did successfully..!!          

Flowchart:

Flowchart: Replace a specific line in a file with a new text

C Programming Code Editor:

Take another style to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a program in C to delete a specific line from a file.
Side by side: Write a programme in C to append multiple lines at the finish of a text file.

What is the difficulty level of this exercise?

Exam your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)?

They are the same when used for output, e.1000. with printf.

However, these are different when used as input specifier e.g. with scanf, where %d scans an integer as a signed decimal number, but %i defaults to decimal but also allows hexadecimal (if preceded by 0x) and octal (if preceded by 0).

So 033 would exist 27 with %i but 33 with %d.

Ref : https://bit.ly/3cyW85P


  • New Content published on w3resource:
  • HTML-CSS Practical: Exercises, Exercise, Solution
  • Java Regular Expression: Exercises, Practice, Solution
  • Scala Programming Exercises, Practise, Solution
  • Python Itertools exercises
  • Python Numpy exercises
  • Python GeoPy Packet exercises
  • Python Pandas exercises
  • Python nltk exercises
  • Python BeautifulSoup exercises
  • Course Template
  • Composer - PHP Bundle Managing director
  • PHPUnit - PHP Testing
  • Laravel - PHP Framework
  • Athwart - JavaScript Framework
  • Vue - JavaScript Framework
  • Jest - JavaScript Testing Framework


Source: https://www.w3resource.com/c-programming-exercises/file-handling/c-file-handling-exercise-9.php

Posted by: jamesfarinell1998.blogspot.com

0 Response to "How To Change One Specific Line Of A File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel