Function Counter: Counting # of Functions in a File - Tips & Ideas

AI Thread Summary
To count the number of functions in a C file, one effective method is to read the file one token at a time, focusing on the opening and closing braces. By maintaining a counter that increments for each '{' and decrements for each '}', the number of functions can be determined by tracking when the counter transitions from zero to one. While a basic approach involves using simple character reading, utilizing regular expressions could offer a more robust solution for parsing. Additionally, reading the file one character at a time may simplify the process of identifying function boundaries. This discussion emphasizes the importance of tokenization and the potential benefits of different reading strategies in C programming.
Stanley_Smith
Messages
16
Reaction score
0
Hi everyone,

I need to write a C code program to count the # of fuctions used in a certain file (function counter)

Can you guys give me some hints/suggestions/ideas ?

Thanks,
Stan
 
Physics news on Phys.org
Do you know how to read in a file one token at a time? You should be able to read each { and } symbol as an independent token. If you keep a running count of the braces, adding one for each { and subtracting one for each }, the number of functions in the file is the number of times the counter goes from zero to one.

Of course, this is primitive -- in the real world, I would probably use regular expressions to match each line of the .cpp file.

- Warren
 
Thanks, Warren for the response.
Can you please show me how to read in a file one token at a time...
 
Stanley,

Something like this:

PHP:
#include <iostream>

int main( int argc, char** argv )
{
    char str[100];
    
    // don't read more than 100 characters
    // and overflow the buffer
    cin.width(100); 

    while( !cin.eof() )
    {
        cin >> str;
        ...
    }

    return 0;
}
 
You could use the string class, instead of a char*, so you don't have to have an upper bound on the size of a read.

But that doesn't read it in one token at a time, does it? Isn't, for example wouldn't,

{return(0);}

be something like 7 tokens? But they'd all be read in at once.


Wouldn't it be conceptually easier to process the file one character at a time?
 
Yeah, true, Hurkyl, I don't know how the istream class treats punctuation. You're probably right, it doesn't treat (){}* etc. as delimiters.

You're right, reading one character at a time might be easier.

- Warren
 
Thread 'Collision of a bullet on a rod-string system: query'
In this question, I have a question. I am NOT trying to solve it, but it is just a conceptual question. Consider the point on the rod, which connects the string and the rod. My question: just before and after the collision, is ANGULAR momentum CONSERVED about this point? Lets call the point which connects the string and rod as P. Why am I asking this? : it is clear from the scenario that the point of concern, which connects the string and the rod, moves in a circular path due to the string...
Thread 'A cylinder connected to a hanged mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top