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

Click For Summary
SUMMARY

The discussion focuses on creating a C program to count the number of functions in a file by tracking the opening and closing braces. Warren suggests reading the file one token at a time, using a counter that increments for each '{' and decrements for each '}'. He also mentions that using regular expressions could be a more advanced approach for parsing .cpp files. The conversation highlights the importance of understanding file reading techniques in C, particularly the use of the istream class and character processing.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with file I/O operations in C
  • Knowledge of tokenization and parsing techniques
  • Basic understanding of regular expressions
NEXT STEPS
  • Implement a function to read a file character by character in C
  • Explore regular expressions in C++ for advanced parsing
  • Learn about the C++ string class for dynamic string handling
  • Study the differences between token-based and character-based file processing
USEFUL FOR

C developers, software engineers working with C/C++ file processing, and anyone interested in function counting and parsing techniques in 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
 

Similar threads

Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 11 ·
Replies
11
Views
980
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K