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

Click For Summary

Homework Help Overview

The original poster seeks assistance in writing a C program to count the number of functions in a specified file. The discussion revolves around methods for reading file content and identifying function definitions.

Discussion Character

  • Exploratory, Conceptual clarification, Problem interpretation

Approaches and Questions Raised

  • Participants discuss reading a file one token at a time and suggest counting braces to determine function occurrences. There are questions about the effectiveness of different reading methods, including the use of character versus token-based approaches.

Discussion Status

Some participants have offered guidance on reading files and counting tokens, while others are exploring the implications of different methods. There is an ongoing exchange of ideas without a clear consensus on the best approach.

Contextual Notes

Participants are considering constraints related to file reading methods and the treatment of punctuation in programming languages, which may affect how functions are identified.

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
13K
Replies
6
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K