C Read File Into Array of Strings

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

C File Treatment : Exercise-4 with Solution

Write a program in C to read the file and shop the lines into an assortment.

Sample Solution:

C Code:

            #include <stdio.h> #include <stdlib.h> #include <string.h>  #define LSIZ 128  #define RSIZ ten   int primary(void)  {     char line[RSIZ][LSIZ]; 	char fname[20];     FILE *fptr = NULL;      int i = 0;     int tot = 0;     printf("\n\northward Read the file and store the lines into an array :\n"); 	printf("------------------------------------------------------\n");  	printf(" Input the filename to be opened : "); 	scanf("%s",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 %south  are : \northward",fname);         for(i = 0; i < tot; ++i)     {         printf(" %s\north", line[i]);     }     printf("\n");     return 0; }                      

Sample Output:

            Read the file and shop the lines into an array :                                                             ------------------------------------------------------                                                          Input the filename to exist opened : test.txt                                                                                                                                                                                    The content of the file test.txt  are :                                                                                                                                                                                       test line 1                                                                                                    test line two                                                                                                    test line 3                                                                                                    test line 4          

Flowchart:

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

C Programming Code Editor:

Have some other 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?

Exam your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

C Programming - Different methods to impress 'Hello world' without using semicolon in C

How to print any bulletin like "Hello earth" or how to execute a printf statement in C plan without using semicolon.

This article contains some of the methods past using them you tin can execute any printf argument to impress whatever bulletin on the screen without using semicolon (which is a special graphic symbol in C programming language and it is used to terminate the executable argument in C language).

Using if statement

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

Using loop statement

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

Using switch statement

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

Using Macro Definition

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

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


hettingerantrume1988.blogspot.com

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

0 Response to "C Read File Into Array of Strings"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel