Strings

I have a current assignment where I need to take lets say a chunk of code and be able to analysis it line by line to see what percentage is C programming and C++. I looking at this step by step so I am wondering what the best way to i'm guessing use strings to look at a bunch of code so I can next step pick out particular characteristics.

First this how do I get me code to read lets say a paragraph or program code?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>
#include <string>

ifstream inputFile("C:/source.c")  //ofcourse, put your own directory

string a;
inputFile>>a;   //read 1 string from file separated by space

while(!inputFile.eof())   //read the whole source file to the end
{
    inputFile>>a;
    //do what you want with a
}


Let us see how you pick up from here

Hmm that is an odd assignment.

Is int considered C? C++? Or both? int came before C++.

The other thing is having to write a dictionary for every C and C++ function would take, a while to say the least. I'm guessing your only option is to scan through said file yourself and figure out which is C and C++ first then just write a parser for those few functions.

Ironically last night I wrote something that had to search text for specific strings, let me find it out and modify it to give you a basic example of what I think you want.

1
2
3
4
5
6
7
8
9
std::ifstream ifs("Example.code");

while(ifs) // while the file is open
{
	std::string line;
	std::getline(ifs, line); // Get each line
	if(line.find("FILE*") != std::string::npos) C++;
	else if(line.find("fstream") != std::string::npos) CPP++;
}
Last edited on
Both are really good. I'm still learning so the whole getting started is the toughest area for me but once I get started it does seem to flow so I will keep posting what I come up with you see what you guys think.
ifstream inputFile("C:/source.c") // notice '/' vs '\' , later is used in windows!
Would I put these into the main or should I put in a function
It depends on how you want to structure your program. I'll not read file as a separate function here coz it does not make much sense to.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    //have already created inputFile and all variables
    while(!inputFile.eof())
    { 
         inputFile>>word;  //no need writing this a function.
         if("word is a C keyword")
         { 
             cWords++;
         }
         if("word is a C++ keyword")  //notice how i use if, not else. Coz word could be C and C++
         {
             c++Words++;
         }
         totalWords++;
     }
}
Last edited on
Ok this may make more sense the rest of the problem is to look for certain things and break down the file

such as
Number of total lines in the file – you need this for percentages.
Percentage of blank lines
Percentage of statements, as figured by counting semicolons (;)
Percentage of blocks, as figured by counting braces ({ and })
Percentage of pointer references, as figured by counting “->”
Percentage of comment lines, including both // and /* / style comments. Bear in mind that either type of comment can appear in either type of file, and that you should count each line that occurs between a /* and a */ as a comment line.
Topic archived. No new replies allowed.