Javascript required
Skip to content Skip to sidebar Skip to footer

C++ Read Data From Texfile Next Line

C Exercises: Write multiple lines in a text file and read the file

C File Handling : Practise-3 with Solution

Write a program in C to write multiple lines in a text file.

Sample Solution:

C Code:

            #include <stdio.h>  int chief () {   FILE * fptr;   int i,n;   char str[100];   char fname[20]="test.txt";   char str1;        printf("\n\n Write multiple lines in a text file and read the file :\n"); 	printf("------------------------------------------------------------\north");    	printf(" Input the number of lines to be written : "); 	scanf("%d", &n); 	printf("\due north :: The lines are ::\n"); 	fptr = fopen (fname,"w");  	for(i = 0; i < n+1;i++) 		{ 		fgets(str, sizeof str, stdin); 		fputs(str, fptr); 		}   fclose (fptr); /*-------------- read the file -------------------------------------*/ 	fptr = fopen (fname, "r");   	printf("\northward The content of the file %s is  :\due north",fname); 	str1 = fgetc(fptr); 	while (str1 != EOF) 		{ 			printf ("%c", str1); 			str1 = fgetc(fptr); 		}     printf("\n\n");     fclose (fptr);     render 0; }                      

Sample Output:

            Write multiple lines in a text file and read the file :                                                       ------------------------------------------------------------                                                    Input the number of lines to exist written : 4                                                                                                                                                                                   :: The lines are ::                                                                                           exam line ane                                                                                                    test line two                                                                                                    test line three                                                                                                    exam line four                                                                                                                                                                                                                    The content of the file test.txt is  :                                                                                                                                                                                       test line ane                                                                                                    test line two                                                                                                    test line iii                                                                                                    examination line 4          

Flowchart:

Flowchart: Write multiple lines in a text file and read the file

C Programming Lawmaking Editor:

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

Previous: Write a program in C to read an existing file.
Next: Write a program in C to read the file and store the lines into an array.

What is the difficulty level of this exercise?

Examination your Programming skills with w3resource's quiz.



C Programming: Tips of the 24-hour interval

C Programming - What is the difference betwixt const int*, const int * const, and int const *?

Read it backwards (as driven by Clockwise/Spiral Dominion):

  • int* - pointer to int
  • int const * - arrow to const int
  • int * const - const pointer to int
  • int const * const - const arrow to const int

At present the first const tin can be on either side of the type so:

  • const int * == int const *
  • const int * const == int const * const

If you want to go really crazy you can practice things like this:

  • int ** - pointer to arrow to int
  • int ** const - a const arrow to a arrow to an int
  • int * const * - a pointer to a const pointer to an int
  • int const ** - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int
  • ...

And to make sure we are articulate on the meaning of const:

int a = 5, b = 10, c = 15;  const int* foo;     // pointer to abiding int. foo = &a;           // consignment to where foo points to.  /* dummy argument*/ *foo = 6;           // the value of a can't get inverse through the pointer.  foo = &b;           // the pointer foo tin can be changed.    int *const bar = &c;  // constant arrow to int                        // annotation, you really need to gear up the pointer                        // hither because yous tin can't change information technology later ;)  *bar = 16;            // the value of c can be changed through the pointer.      /* dummy argument*/ bar = &a;             // not possible because bar is a constant pointer.          

foo is a variable pointer to a constant integer. This lets you change what you lot point to but non the value that you betoken to. Most often this is seen with C-style strings where you have a pointer to a const char. Y'all may change which string you point to but you can't change the content of these strings. This is important when the cord itself is in the information segment of a program and shouldn't exist changed.
bar is a constant or stock-still arrow to a value that can be inverse. This is like a reference without the actress syntactic sugar. Because of this fact, usually you would utilize a reference where yous would use a T* const arrow unless y'all need to allow NULL pointers.

Ref : https://chip.ly/39RctBb


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


C++ Read Data From Texfile Next Line

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