Read File Line by Line and Store in Array C++
C Exercises: Read the file and store the lines into an array
C File Handling : Exercise-iv with Solution
Write a programme in C to read the file and store the lines into an assortment.
Sample Solution:
C Code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define LSIZ 128 #define RSIZ 10 int main(void) { char line[RSIZ][LSIZ]; char fname[20]; FILE *fptr = NULL; int i = 0; int tot = 0; printf("\north\n Read the file and store the lines into an array :\northward"); printf("------------------------------------------------------\northward"); printf(" Input the filename to exist opened : "); scanf("%south",fname); fptr = fopen(fname, "r"); while(fgets(line[i], LSIZ, fptr)) { line[i][strlen(line[i]) - one] = '\0'; i++; } tot = i; printf("\n The content of the file %southward are : \n",fname); for(i = 0; i < tot; ++i) { printf(" %s\northward", line[i]); } printf("\due north"); return 0; }
Sample Output:
Read the file and shop the lines into an array : ------------------------------------------------------ Input the filename to be opened : test.txt The content of the file test.txt are : test line ane exam line two test line 3 test line iv
Flowchart:

C Programming Code Editor:
Have some other style 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.
Side by side: Write a programme 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'due south quiz.
C Programming: Tips of the Solar day
C Programming - How to separate 'words' while declaring an identifier in C language?
How to declare an identifier name that has more than i discussion (multi-words variable/identifier declaration)?
When, you are declaring an identifier similar variable name, function name etc and there are two words in information technology. You lot should separate them by using underscore (_) or mixed capital and lowercase alphabets.
For case: if in that location is a variable to store production weight, variable identifier/variable proper name should be: product_weight, productWeight, product_Weight
Consider the below case: here we are reading product name and production weight and and then press them (it's just a simple plan that will read and print the value; you have to focus on variable naming conventions).
#include <stdio.h> int chief() { char product_name[100]={0}; float product_weight; printf("Enter production name: "); scanf("%[^\northward]south",product_name); printf("Enter production weight: "); scanf("%f",&product_weight); printf("Production details:\n"); printf("Proper name: %due south\nWeight: %.02f\n",product_name,product_weight); render 0; }
Output:
Enter production proper name: Lenovo B40 Laptop Enter product weight: two.one Product details: Name: Lenovo B40 Laptop Weight: ii.10
Do yous know why we used "% .02f" to impress the float value?
%.02f - will print the bladder value till ii decimal places with zero (0) padding.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Exercise, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Parcel exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Grade Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework
mitchellfackeffaced.blogspot.com
Source: https://www.w3resource.com/c-programming-exercises/file-handling/c-file-handling-exercise-4.php
Belum ada Komentar untuk "Read File Line by Line and Store in Array C++"
Posting Komentar