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
 
Kindly see the attached pdf. My attempt to solve it, is in it. I'm wondering if my solution is right. My idea is this: At any point of time, the ball may be assumed to be at an incline which is at an angle of θ(kindly see both the pics in the pdf file). The value of θ will continuously change and so will the value of friction. I'm not able to figure out, why my solution is wrong, if it is wrong .
TL;DR Summary: I came across this question from a Sri Lankan A-level textbook. Question - An ice cube with a length of 10 cm is immersed in water at 0 °C. An observer observes the ice cube from the water, and it seems to be 7.75 cm long. If the refractive index of water is 4/3, find the height of the ice cube immersed in the water. I could not understand how the apparent height of the ice cube in the water depends on the height of the ice cube immersed in the water. Does anyone have an...
Back
Top