Answer» hello everyone.
following the subject. i got problems about counting lines of code program. the CONDITIONS of counting are: - the program must not count the comment lines. both // and /* */ - the program must not count the space line. - count how many function in each code.
i want something like the way you distinguish the comment lines, the space lines from others. THX.
PS. my English might weird, i'm not English native speaker.by the way someone told, to use stack is easy for solving this problem. anybody please help me. i tried in many ways but it still doesn't work.What programming language?c language
plz help meI am not going to tell you every SINGLE thing, but this might get you started. I don't have a compiler to TEST on this computer, so I might have made a few minor errors.
Code: [Select]#include <stdio.h> #include <string.h>
int main ( void ) { int lineCount = 0; static const char filename[] = "file.txt"; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 128 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { if(strcmp (line, "") != 0) //If line not empty { if(strstr(line,"//") == NULL && strstr(line,"/*") == NULL) lineCount++; } } fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } return 0; } Many here will help you. One way to do the job is break it into parts. Here is part of the answer. http://bytes.com/topic/c/answers/214974-program-remove-c-comments-long-signature The NEW file will have only lines of code. Lines are easy to count in DOS prompt. thank you so much for your help i'll try to understand this code tomorrow (now it's 1.18 am.) and i hope my problem will be getting easier. really thanks.I forgot, you will have to print out the lineCount after the program finishes reading the file, but you should know how to use printf.@Linux711 yes,i know how to use printf. @Geek-9pm thx
|