C Read Data From File and Return Line Count
C Exercises: Read the file and count the number of lines
C File Treatment : Exercise-5 with Solution
Write a plan in C to Find the Number of Lines in a Text File.
Sample Solution:
C Lawmaking:
#include <stdio.h> #define FSIZE 100 int main() { FILE *fptr; int ctr = 0; char fname[FSIZE]; char c; printf("\north\north Read the file and count the number of lines :\n"); printf("--------------------------------------------------\north"); printf(" Input the file proper name to be opened : "); scanf("%s",fname); fptr = fopen(fname, "r"); if (fptr == Nix) { printf("Could non open up file %s", fname); return 0; } // Extract characters from file and store in character c for (c = getc(fptr); c != EOF; c = getc(fptr)) if (c == '\northward') // Increment count if this character is newline ctr = ctr + one; fclose(fptr); printf(" The lines in the file %due south are : %d \northward \due north", fname, ctr-1); return 0; }
Sample Output:
Read the file and count the number of lines : -------------------------------------------------- Input the file name to be opened : examination.txt The lines in the file examination.txt are : 4
Flowchart:
C Programming Lawmaking Editor:
Accept another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a plan in C to read the file and store the lines into an array.
Next: Write a program in C to find the content of the file and number of lines in a Text File.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource'southward quiz.
C Programming: Tips of the Mean solar day
C Programming - What is the departure between const int*, const int * const, and int const *?
Read information technology backwards (as driven by Clockwise/Spiral Rule):
- int* - pointer to int
- int const * - pointer to const int
- int * const - const pointer to int
- int const * const - const pointer to const int
Now the outset const can be on either side of the blazon and then:
- const int * == int const *
- const int * const == int const * const
If you want to go actually crazy you lot can practice things like this:
- int ** - pointer to pointer to int
- int ** const - a const arrow to a pointer to an int
- int * const * - a pointer to a const pointer to an int
- int const ** - a pointer to a arrow to a const int
- int * const * const - a const pointer to a const pointer to an int
- ...
And to brand sure we are clear on the meaning of const:
int a = 5, b = ten, c = 15; const int* foo; // arrow to constant int. foo = &a; // assignment to where foo points to. /* dummy statement*/ *foo = 6; // the value of a tin't get inverse through the arrow. foo = &b; // the pointer foo can be changed. int *const bar = &c; // constant pointer to int // note, yous really need to set the pointer // here because you can't alter information technology afterward ;) *bar = xvi; // the value of c can exist changed through the pointer. /* dummy statement*/ bar = &a; // non possible considering bar is a constant pointer.
foo is a variable pointer to a abiding integer. This lets you change what you indicate to but not the value that you point to. Most often this is seen with C-style strings where you lot accept a pointer to a const char. Yous may change which string y'all point to but you lot can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't exist changed.
bar is a constant or fixed pointer to a value that tin be changed. This is like a reference without the extra syntactic saccharide. Considering of this fact, commonly you would use a reference where yous would use a T* const arrow unless you need to permit Nada pointers.
Ref : https://flake.ly/39RctBb
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Coffee Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Do, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Packet 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
Source: https://www.w3resource.com/c-programming-exercises/file-handling/c-file-handling-exercise-5.php
0 Response to "C Read Data From File and Return Line Count"
Post a Comment