i have a problem when count something in a text file with 1. like done a work and write 1 in the text file. but the problem arise when i want to count those. here i want to share a somple C programming code. its very simple task. i had done this kind of programming at 1st semister. but after 2yrs i need this code :)
any wany here is the code:
#include <stdio.h>
#include <conio.h>
int main(int argc, const char *const *argv)
{
clrscr();
FILE *file = fopen(“count.txt”, “r”);
if ( file )
{
int ch, prev = ‘0′ /* so empty files have no lines */, text = 0;
while ( (ch = fgetc(file)) != EOF ) /* Read all chars in the file. */
{
if ( ch == ‘1′ )
{
text++; /* Bump the counter for every text. */
}
prev = ch; /* Keep a copy to later test whether… */
}
fclose(file);printf(“lines = %d\n”, text);
}getch();
return 0;
}
copy this and paste to the root directories. and enjoy counting
here is my counting file count.txt download here the file.








No comments yet
Comments feed for this article