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

Dani AI

Generated

asked for counting only the code regions marked with //DO; pointed in the right direction with a line-oriented scan and scope tracking. Two practical interpretations and solutions are worth highlighting:

  • Marker-delimited blocks (the sample has two //DO comments that bracket the block). Treat each //DO as a delimiter and count the lines between the delimiters (inclusive or exclusive per need). This is simple and robust when source already contains both start and end markers.
  • Marker-annotated scopes (a single //DO labels the following scope). This requires locating the scope start (the nearest un-commented '{' or the single statement that follows) and then tracking nested braces while ignoring braces that appear inside strings or comments.

The compact C++ example below implements the marker-delimited mode (matches the sample and yields 4 for the shown do-block). It tolerates extra spaces and reports partial blocks if an end marker is missing.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

vector<int> countDelimitedBlocks(const string& path, const string& marker = "//DO") {
    ifstream in(path);
    if (!in) return {};
    string line;
    bool inBlock = false;
    int cur = 0;
    vector<int> results;
    while (getline(in, line)) {
        if (line.find(marker) != string::npos) {
            if (!inBlock) { inBlock = true; cur = 1; }    // include opening marker line
            else         { cur++; results.push_back(cur); inBlock = false; cur = 0; } // include closing marker
        } else if (inBlock) {
            cur++;
        }
    }
    if (inBlock) results.push_back(cur); // unmatched start marker -> partial block
    return results;
}

int main(int argc, char** argv) {
    for (int i = 1; i < argc; ++i) {
        auto blocks = countDelimitedBlocks(argv[i], "//DO");
        for (size_t k = 0; k < blocks.size(); ++k)
            cout << argv[i] << "  block " << (k+1) << ": " << blocks[k] << " lines\n";
    }
}

Notes and tips for the annotated-scope case: implement a small lexer that scans characters and keeps flags for in-line comments, block comments, single-quote and double-quote literals; only count braces when none of those flags are set. Also account for single-statement bodies (no braces). Test with edge cases: braces inside strings, multi-line comments, preprocessor macros, Windows CRLF, and files with unmatched markers. The marker-delimited approach is easiest when source already has explicit start/end comments; annotated-scope needs the extra lexer to be reliable.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.