How to count line of code in text file or file .cpp
(this .txt file or .cpp file is the input file of program)

and what's function for count line of only mark code

for example of this code(file input) , I want to count only "do" function that have mark //DO
The answers of program for read this code have to show "4"

#include <stdio.h>
int main ()
{
  FILE * pFile;
  int c;
  int n = 0;
  pFile=fopen ("1818.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    do { //DO
      c = fgetc (pFile);
      if (c == '$') n++;
    }    //DO
      while (c != EOF);
    fclose (pFile);
    printf ("File contains %d$.\n",n);
  }
  return 0;
}

what's the source code for count the line of code and the line of each function

,Thank you

How to count line of code in text file or file .cpp
(this .txt file or .cpp file is the input file of program)

and what's function for count line of only mark code

for example of this code(file input) , I want to count only "do" function that have mark //DO
The answers of program for read this code have to show "4"

#include <stdio.h>
int main ()
{
  FILE * pFile;
  int c;
  int n = 0;
  pFile=fopen ("1818.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    do { //DO
      c = fgetc (pFile);
      if (c == '$') n++;
    }    //DO
      while (c != EOF);
    fclose (pFile);
    printf ("File contains %d$.\n",n);
  }
  return 0;
}

what's the source code for count the line of code and the line of each function

,Thank you

You could go through the file line by line using the getline function. You could have a line number count variable that would be initialized at 0 and is incremented each time getline is called. For each line read in, use the "find" function from the string library to look for "//DO". Once that string is found, start another three counters. One counter will keep track of the number of beginning brackets you have seen. One counter will keep track of the number of ending brackets you have seen. The third counter will be initialized at 0 and incremented for every line read in until the number of beginning brackets and ending brackets are the same, thus signifying the number of lines inside the do-while function.

This article has been dead for over six months. Start a new discussion instead.