My Tech notes: C Program for Counting Number of lines from File
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Sunday, September 21, 2008

C Program for Counting Number of lines from File

C program for counting number of lines from a file.
I was in need to count no of lines from a file,
written simple program to do that.

int no_of_lines()
{
int i=0,j=0; /* declare a char array */
FILE *file; /* declare a FILE pointer */
char *line;
char *temp=NULL;

file = fopen("/rdisk0/var/log/nas2msg", "r");
/* open a text file for reading */

if(file==NULL) {
return 9;
}
else {
while( (i=getc(file)) !=EOF) {
/* print the file one line at a time */
//i=getc(file);
if(i == '\n')
printf("line %d",j++);
}
fclose(file);
return j;
}
}

No comments:

Post a Comment