Home Forums C Programming How to count characters from text file Reply To: How to count characters from text file

#3374
Priyansh Agrawal
Participant

Count each line, voila: (you can pass the filename/path as the first argument)

#include 

int main(int argc,char **argv)
{
const int max=1024;
int r=0,count=0,i;
FILE *f;
char buff[max+3];

if (argc==2)
{
if ((f=fopen(argv[1],"rb"))==NULL)
{
r=2;
printf("cannot open file!n");
}
else
{
while (!feof(f))
{
buff[0]=0;
fgets(buff,max,f);
count++;

for (i=0;i
printf("Line %i, count %in",count,i);
}

fclose(f);
}
}
else
{
r=1;
printf("usage: count n");
}

return r;
}