Number of line breaks and space in a file
#include <stdio.h>
int main(void){
FILE *fpt; // file pointer
char str;
int i,lb=0,sp=0;
fpt=fopen("data2.txt","r"); // file already exit for reading
if(fpt==NULL)
printf("Can't open file ");
else {
while(str != EOF){
str=getc(fpt);
if(str=='\n'){
lb=lb+1;
}
if(str==' '){
sp=sp+1;
}
}
fclose(fpt);
}
printf("No of line breaks : %d \n",lb);
printf("No of space : %d \n",sp);
return 0;
}
data2.txt file is here
welcome to plus2net
Next Line
Output is here
No of line breaks : 1
No of space : 6
← File Handling in C
This article is written by plus2net.com team.
plus2net.com