/**********************************************************
* Program to interactively ask the user to provide
* a file name, and then do following tasks.
*
* 1. open to file to read
* 2. count the number of lines in the file.
* 3. display file and the count of lines.
* 4. Repeal until user ends the program.
// to open a file there are two ways
//1.Put the file inside the project where the main.cpp file exists
//then when the program ask you to enter a file name, you need
//to write a file name that you had written in the main.cpp file exists somefile.cpp
//for example: "somefile.cpp" and you write (string fileName = "somfile.cpp";) inside your int main( ) code
//2. Another way is using absolute path: say you have a file somefile.cpp
//or somefile.txt or any fileÖ in c drive, then enter c:/somefile.txt ÖÖetc.
*********************************************************/
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
//struct to represent a node in the list
typedefstruct Node
{
string line;
Node * next; //next pointer
};
//function to read the file line by line
//store the lines into the list nodes
//count the number of lines and return the
//line count.
int countLOC(ifstream & file);
int countFunloc(ifstream & file);
//main method
int main()
{
//cout << " my function name is " << __FUNCTION__<< endl;
string fileName = "";
do
{
//prompt the user to enter the file name
cout << "Enter file name to count lines or quit: ";
cin >> fileName;
cout << endl;
//check if its a quit otherwise count lines
if(fileName != "quit")
{
//open file stream
ifstream in(fileName.c_str(), ios::in);
//check if file is open to read
if(!in.is_open())
{
cout << "ERROR: opening file - " << fileName << endl;
}
else //file is open read the lines and output
{
cout << fileName << " successfully read." << endl << endl;;
cout << fileName << " has " << countLOC(in) << " number of lines." << endl << endl;
ifstream in(fileName.c_str(), ios::in);
cout << countFunloc(in) <<" number of lines " << endl << endl;
// cout << fileName << " has " << countLOC(in) << " number of lines." << endl;
//close the file
in.close();
}
}
else
{
//exit message
cout << "\nGoodBye!! Thank you for using LOC Program\n";
}
cout << endl;
}while(fileName != "quit");
system("pause");
return 0;
}
int countLOC(ifstream & file){
// cout <<" my function name is "<<__FUNCTION__<< endl;;
//list head node
Node * head = NULL;
string line = "";
int count = 0;
//read the file line by line
while(!file.eof())
{
//cout << "Moad" << endl;
//read a line from file
getline(file, line);
//check if successfully read
if(file)
{
//check if is a blank line or comment line
if(line.empty() || line.find("//") != string::npos)
{
//do nothing ignore the line
}
else
{
//create new Node
Node * newNode = new Node;
newNode->line = line;
newNode->next = NULL;
//store into list
if(head == NULL)
{
head = newNode;
}
else
{
//find the last node
Node * cur = head;
while(cur->next)
cur = cur->next;
cur->next = newNode;
cur->next->next = NULL;
}
count++;
}
}
}
//return line count
return count;
}
int countFunloc(ifstream & file){
//list head node
Node * head = NULL;
//bool bol = true;
int flag,flag1=0;
string line = "";
int count = 0;
//read the file line by line
while(!file.eof())
{
flag=0;
//cout << "Moad" <<endl;
//read a line from file
getline(file, line);
//check if successfully read
if(file)
{
//check if is a blank line or comment line
if(line.empty() || line.find("//") != string::npos)
{
//do nothing ignore the line
}
if(flag1 == 1)
count++;
if(line.find("float") !=string::npos || line.find("void") !=string::npos || line.find("int") !=string::npos && line.find("(") !=string::npos &&(line.find)(")") !=string::npos && line.find("{") !=string::npos)
//flag=1;
// if(line.find("in.close")==string::npos && line.find("system")==string::npos && line.find("getline")==string::npos && line.find("cout")==string::npos && line.find("for")==string::npos && line.find("while")==string::npos && line.find("switch")==string::npos && line.find("if")==string::npos)
{ flag1=1;
cout <<" The function Name is "<< line << endl << endl;
count++;}
if(line.find("}")!=string::npos)
{
//flag
flag1 =0;
}//}
}
}
//return line count
cout <<" The number of line is ";
return count;
}