Nalla said:
Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...
Since we don't do people's work for them here, I'll offer
a more general solution that you should be able to adapt
to your specific needs. This shows the line number where
each directive appears (and the line itself), and how many
lines appear between successive directives. A directive is
considered to exist on any line whose first non-whitespace
character is a '#' character.
#define COMPILE_THIS
#ifdef COMPILE_THIS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_SIZE 128
#define DIRECTIVE_CHAR '#'
int is_directive(const char *line, size_t len)
{
static char tok[LINE_SIZE];
return sscanf(line, "%s", tok) != EOF &&
*tok == DIRECTIVE_CHAR;
}
int main(int argc, char **argv)
{
char line[512] = {0};
size_t line_no = 0;
size_t between = 0;
FILE *input = NULL;
if(argc < 2)
{
puts("Requires file name argument");
return EXIT_FAILURE;
}
if(!(input = fopen(argv[1], "r")))
{
puts("Cannot open input\n");
return EXIT_FAILURE;
}
while(fgets(line, sizeof line, input))
{
++line_no;
if(is_directive(line, sizeof line))
{
if(between)
printf("[%lu line%c]\n",
(unsigned long)between, " s"[between > 1]);
printf("[Line %lu] %s", (unsigned long)line_no, line);
between = 0;
}
else
++between;
}
fclose(input);
return 0;
}
#endif
Output when given its own source:
[Line 1] #define COMPILE_THIS
[2 lines]
[Line 4] #ifdef COMPILE_THIS
[1 line ]
[Line 6] #include <stdio.h>
[Line 7] #include <stdlib.h>
[Line 8] #include <string.h>
[1 line ]
[Line 10] #define LINE_SIZE 128
[Line 11] #define DIRECTIVE_CHAR '#'
[48 lines]
[Line 60] #endif
I did not test this thoroughly; I'll let you discover
and fix any bugs that might exist.
HTH,
-Mike