Skip to content Skip to sidebar Skip to footer

How to Read Data From a File to an Array in C++

C Exercises: Read the file and store the lines into an array

C File Handling : Exercise-4 with Solution

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

Sample Solution:

C Lawmaking:

            #include <stdio.h> #include <stdlib.h> #include <cord.h>  #define LSIZ 128  #define RSIZ 10   int main(void)  {     char line[RSIZ][LSIZ]; 	char fname[xx];     FILE *fptr = NULL;      int i = 0;     int tot = 0;     printf("\n\northward Read the file and store the lines into an array :\northward"); 	printf("------------------------------------------------------\n");  	printf(" Input the filename to be opened : "); 	scanf("%southward",fname);	      fptr = fopen(fname, "r");     while(fgets(line[i], LSIZ, fptr))  	{         line[i][strlen(line[i]) - 1] = '\0';         i++;     }     tot = i; 	printf("\n The content of the file %southward  are : \due north",fname);         for(i = 0; i < tot; ++i)     {         printf(" %south\n", line[i]);     }     printf("\n");     return 0; }                      

Sample Output:

            Read the file and store the lines into an assortment :                                                             ------------------------------------------------------                                                          Input the filename to be opened : test.txt                                                                                                                                                                                    The content of the file test.txt  are :                                                                                                                                                                                       examination line ane                                                                                                    examination line 2                                                                                                    test line 3                                                                                                    test line 4          

Flowchart:

Flowchart: Read the file and store the lines into an array

C Programming Code Editor:

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

Previous: Write a program in C to write multiple lines in a text file.
Next: Write a program in C to Find the Number of Lines in a Text File.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Mean solar day

C Programming - Different methods to print 'Hullo world' without using semicolon in C

How to impress any bulletin 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 execute any printf statement to print any message on the screen without using semicolon (which is a special character in C programming language and it is used to finish the executable statement in C linguistic communication).

Using if statement

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

Using loop statement

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

Using switch statement

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

Using Macro Definition

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

  • New Content published on w3resource:
  • HTML-CSS Practical: Exercises, Practise, Solution
  • Java Regular Expression: Exercises, Practice, 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 Package Manager
  • PHPUnit - PHP Testing
  • Laravel - PHP Framework
  • Angular - JavaScript Framework
  • Vue - JavaScript Framework
  • Jest - JavaScript Testing Framework


schmidtnurreacuot.blogspot.com

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

Post a Comment for "How to Read Data From a File to an Array in C++"