Hi all! Please give c code which finds the included header files of another c file

Recommended Answers

All 7 Replies

Hi all! Please give c code which finds the included header files of another c file

hi shankamita,try the following code

#include<stdio.h>
#include<string.h>
void main()
{
	FILE *fp;
	char c[50];
	int count=0;
	clrscr();
	fp=fopen("hello.c","r");
	while(!feof(fp))
	{
		fscanf(fp,"%s",c);
		if(strstr(c,"#include"))
		     count++;
	}
	printf("number of files included are %d\n",count);
	getch();
}

i hope i wud be useful to u...

Your program has at least a few problems
>>void main()

It's int main(), even though ancient compilers such as Turbo C allow it.


>>while(!feof(fp))
eof() doesn't work like that and will cause that loop to execute too many times.

>>strstr
If you are going to use scanf() then there is no point using strstr() because the character buffer will only contain one word. strstr() is a waste of time in this situation. Just use strcmp().

Also the OP wants to know the names of the header files not the count .....

If the objective of OP is to prevent including the same header file many times then check out this link

Your program has at least a few problems
>>void main()

It's int main(), even though ancient compilers such as Turbo C allow it.


>>while(!feof(fp))
eof() doesn't work like that and will cause that loop to execute too many times.

>>strstr
If you are going to use scanf() then there is no point using strstr() because the character buffer will only contain one word. strstr() is a waste of time in this situation. Just use strcmp().

Actually i compiled dat code, n it is working wit out any problems...and i saw the syntax while(!feof(filepointer)) from a standard ansi C book... actually the fscanf statement reads the entire line #include<stdio.h>(for instance)..so i use Strstr to check whethr the read string is containing the string #include as a substring or not... I am new to File concepts, so please guide me.. but i checked my code working correctly in TURBO c

Also the OP wants to know the names of the header files not the count .....

If the objective of OP is to prevent including the same header file many times then check out this link

If u want the name of the files den simply remove count++ statement and juz use a printf statement tp print the read string inside the If condition..

if(strstr(c,"#include"))
            printf("%s",c);

>>actually the fscanf statement reads the entire line #include<stdio.h>(
But it will not work if there are any spaces anywhere in that line. Put a space between #include and <stdio.h> and see what happens to your program.

>>but i checked my code working correctly in TURBO c
That's only because turbo c is an ancient, outdated, archaic compiler that doesn't know any better.

As soon as you can manage it, move away from the scanf() and fscanf() functions. They are not robust, and will quit for the smallest variance, sometimes.

the answer is to use:

fgets(stringBuffer, sizeof(stringBuffer), stdin (or filePointer) );

It works remarkably well, the file or the user can NOT over-flow the stringBuffer, and your data is THERE - you can test it 16 ways to Sunday, if need be, to get what you want!

One caveat: If you are in a called function, and use sizeof(YourArray), you WON'T get the size you expect. You'll get the size of the POINTER that you passed to the function. If you want to work with the actual size of the array in a called function, you must pass that number, as a parameter, to the function.


There was a big misunderstanding about feof(). It doesn't work the way a lot of authors and teachers thought it worked. Frequently causing the last data to be processed twice, before it finds the EOF.

Testing your return, inside a while() loop is preferred, and avoids the problem.

Turbo C is more compatible than you think, I think. ;)

I wouldn't recommend it, but if you need to use it, don't worry, you can learn a LOT using it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.