Javascript required
Skip to content Skip to sidebar Skip to footer

Reading and Then Appending to a File C++

C Exercises: Append multiple lines at the finish of a text file

C File Handling : Exercise-x with Solution

Write a plan in C to append multiple lines at the finish of a text file.

Assume that the content of the file test.txt is :                                                                        test line 1                                                                                                    test line 2                                                                                                    exam line 3                                                                                                    test line 4          

Sample Solution:

C Lawmaking:

            #include <stdio.h>  int main () {   FILE * fptr;   int i,n;   char str[100];   char fname[20];   char str1;    	printf("\n\due north Suspend multiple lines at the end of a text file :\n"); 	printf("------------------------------------------------------\n");  	printf(" Input the file name to exist opened : "); 	scanf("%s",fname);		     fptr = fopen(fname, "a");      printf(" Input the number of lines to exist written : ");     scanf("%d", &northward);     printf(" The lines are : \n");       for(i = 0; i < due north+1;i++)     {     fgets(str, sizeof str, stdin);     fputs(str, fptr);   }   fclose (fptr); //----- Read the file later on appended -------  	fptr = fopen (fname, "r");   	printf("\n The content of the file %s is  :\northward",fname); 	str1 = fgetc(fptr); 	while (str1 != EOF) 		{ 			printf ("%c", str1); 			str1 = fgetc(fptr); 		}     printf("\n\due north");     fclose (fptr); //------- End of reading ------------------   return 0; }                      

Sample Output:

            Append multiple lines at the finish of a text file :                                                             ------------------------------------------------------                                                          Input the file name to be opened : examination.txt                                                                    Input the number of lines to be written : 3                                                                    The lines are :                                                                                               test line 5                                                                                                    test line 6                                                                                                    test line vii                                                                                                                                                                                                                    The content of the file examination.txt is  :                                                                                                                                                                                       examination line 1                                                                                                    test line 2                                                                                                    test line 3                                                                                                    test line four                                                                                                                                                                                                                   examination line 5                                                                                                    exam line 6                                                                                                    test line seven          

Flowchart:

Flowchart: Append multiple lines at the end of a text file

C Programming Code Editor:

Have some other way to solve this solution? Contribute your lawmaking (and comments) through Disqus.

Previous: Write a programme in C to replace a specific line with another text in a file.
Next: Write a plan in C to copy a file in some other proper noun.

What is the difficulty level of this practise?

Exam your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

C Programming - Different methods to print 'Howdy earth' without using semicolon in C

How to print any message like "Hello world" or how to execute a printf statement in C program without using semicolon.

This article contains some of the methods by using them you tin can execute whatsoever printf argument to print any message on the screen without using semicolon (which is a special character in C programming linguistic communication and it is used to finish the executable argument in C language).

Using if argument

#include<stdio.h> int main() { 	if(printf("Howdy World")) 	{;} 	 	return 0; }          

Using loop statement

#include<stdio.h> int main() { 	while(!printf("Hello World")) 	{;} 	 	return 0; }          

Using switch statement

#include<stdio.h> int master() { 	switch(printf("Hullo Earth")) 	{ 	} 	 	render 0; }          

Using Macro Definition

#include<stdio.h> #define a printf("Hello World")  int main() {     if(a){;} 	return 0; }          

  • New Content published on w3resource:
  • HTML-CSS Practical: Exercises, Exercise, Solution
  • Java Regular Expression: Exercises, Practice, Solution
  • Scala Programming Exercises, Exercise, Solution
  • Python Itertools exercises
  • Python Numpy exercises
  • Python GeoPy Package exercises
  • Python Pandas exercises
  • Python nltk exercises
  • Python BeautifulSoup exercises
  • Class Template
  • Composer - PHP Parcel Director
  • PHPUnit - PHP Testing
  • Laravel - PHP Framework
  • Angular - JavaScript Framework
  • Vue - JavaScript Framework
  • Jest - JavaScript Testing Framework


hennocked1985.blogspot.com

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