Search code examples
c++filefstream

Creating a program that reads counts the number of lines in a C++ file


im looking to count the number of lines in a code i have figured that i need to look for a specific character from the file and increment by one each time that it is found. Now i have a general gist of it but im having trouble figuring out how to read each line in the file. I have posted what i have so far. Any help would be appreciated

#include<fstream>
#include <iostream>

using namespace std;

 int main()
{
   ifstream readfile;
   readfile.open("project1b.cbp");
   int number_of_lines=0;
   char = ch;
   while(ch !=EOF)
   {
       if(ch!='endl'||';')
        number_of_lines++;
   }


    return 0;
}

Solution

  • You can use std::getline for getting each line.