Write a program to find the number of lines in a text file.
C Program
#include <stdio.h>int main(){FILE *readFile;//empty file have one//lineint n = 0;char filename[10000];char ch;printf("Enter file name with full path: ");scanf("%s", filename);//open file in read modereadFile = fopen(filename, "r");if (readFile == NULL){printf("Can't open file for %s\n", filename);return -1;}else{ch = getc(readFile);while (ch != EOF){//if next lineif (ch == '\n'){n = n + 1;}//take next character from file.ch = getc(readFile);}}//close the filefclose(readFile);printf("Total line is %d ", n);return 0;}
Input:
Enter file name with full path:C:\Users\studentrecord.txt
Input File:studentrecord.txt
Name = Rahul, marks = 67 Name = Rohit, marks = 34 Name = Suman, marks = 65
Output:
Total line is 3
No comments:
Post a Comment