Hi All,
Let me explain the problem.

I have multiple include files as in class1.inc, class2.inc, class3.inc etc. Contents of an include file will be like

class1.inc

{
"john",
12,
68,

"steve",
12,
98,

"mat",
12,
95,

};

This will basically serve as a static array of structures. Here there are three field name(char*), age(int), avg(float).
In my program I want to assign the value of one of these file to a structure variable. My code goes like this

struct std_
{
	char name[50];
	int age;
	float avg;
}
std_str, *std_ptr;

int main(int argc, char **argv)
{
		
	if(!strcmp(argv[2],"class1")
	{
		static std_str obj[200] = 
		#include "class1.inc"
		
		...
	}
	else if(!strcmp(argv[2],"class2")
	{
		...	
	}
return 0;
}

The above code will work fine. But what I want is

int main(int argc, char **argv)
{
	char file[50];

	/* I want to dynamically generate the include file name and include it */
	strcat(file, argv[2]);
	strcat(file, ".inc")
		
	static std_str obj[200] = 
	#include file
	
return 0;
}

But unfortunately, the compilation fails, saying #include expects a file name.

Is there anyway to achieve this?

Thanks and Regards,
Ahamed.

Recommended Answers

Is there anyway to achieve this?
No because the #inc lude directive is processed at compile time, not runtime.

Jump to Post

[QUOTE=ahamed101;1051478]@gerard4143 :
@Dave : The first example is working, the structure gets populated with values in the inc file.
[/quote]
Yes it does work.

[QUOTE=ahamed101;1051478]Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?[/QUOTE]

Read the file …

Jump to Post

[QUOTE=ahamed101;1051478]@gerard4143 : I can't use dlls, I have a restriction.
@Dave : The first example is working, the structure gets populated with values in the inc file.[/QUOTE]sigh

Of course it is. ALL structures of the structures in the first example are populated at compile time. A particular one isn't "selected" …

Jump to Post

[QUOTE=ahamed101;1051539]@twomers

"The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???[/QUOTE]

Yes. Its not really all that big a deal since the program has reserved space for the data anyway. Even though …

Jump to Post

All 14 Replies

Re: Dynamic include in C 80 80

The include statement is processed by the preprocessor so it can't be dynamically created in the executable...

If you want to do this try Dlls

Re: Dynamic include in C 80 80

Your first example may not be working the way you think it is. The #include directive is like a copy-and-paste that happens at compile time. You can't do it at runtime.

Re: Dynamic include in C 80 80

@gerard4143 : I can't use dlls, I have a restriction.
@Dave : The first example is working, the structure gets populated with values in the inc file.

Thanks for the replies.

Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?

Re: Dynamic include in C 80 80

>>Is there anyway to achieve this?
No because the #inc lude directive is processed at compile time, not runtime.

Re: Dynamic include in C 80 80

@gerard4143 :
@Dave : The first example is working, the structure gets populated with values in the inc file.

Yes it does work.

Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?

Read the file into the array just like it was any other normal text file.

Re: Dynamic include in C 80 80

@gerard4143 : I can't use dlls, I have a restriction.
@Dave : The first example is working, the structure gets populated with values in the inc file.

Thanks for the replies.

Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?

I'm not really sure what your after...but you could try working with a marco..

Note - Marcos are processed by the preprocessor as well

Re: Dynamic include in C 80 80

@gerard4143 : I can't use dlls, I have a restriction.
@Dave : The first example is working, the structure gets populated with values in the inc file.

*sigh*

Of course it is. ALL structures of the structures in the first example are populated at compile time. A particular one isn't "selected" in the if tree.

Re: Dynamic include in C 80 80

Actually.
The #include thing only works on compile time. Not run time. So you can't strcat the value of the #include .
Also, your first example mightn't work the way you think.

Re: Dynamic include in C 80 80

@twomers
the first example is working fine.

"The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???

Re: Dynamic include in C 80 80

@twomers

"The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???

Yes. Its not really all that big a deal since the program has reserved space for the data anyway. Even though you have the arrays declared in if statements the compiler will allocate memory for all of them at the same time somewhere in global memory (since they are declared static). The if statements just limit the scope of those arrays.

Re: Dynamic include in C 80 80

This thread is stupid.
ahamed101 -- save the data in files and fopen them according to the input parameters of the program.
OR do what you were doing with the #include and just live with code reuse.

Re: Dynamic include in C 80 80

This thread is stupid.
ahamed101 -- save the data in files and fopen them according to the input parameters of the program.
OR do what you were doing with the #include and just live with code reuse.

I believe no questions are stupid. Please refrain from making such comments in the future.

commented: I agree +25
Re: Dynamic include in C 80 80

>> I believe no questions are stupid.
That's naive. And I didn't say the questions was stupid. I said the thread was stupid.

How many times does the same thing have to be said before it's registered?

Gerard -- "The include statement is processed by the preprocessor so it can't be dynamically created in the executable..."
Dave Sinkula -- "The #include directive is like a copy-and-paste that happens at compile time. You can't do it at runtime."
AD -- "No because the #inc lude directive is processed at compile time, not runtime."
Me (Ironically) >> The #include thing only works on compile time. Not run time.

Then you finally say -- "The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???

Re: Dynamic include in C 80 80

>> I believe no questions are stupid.
That's naive. And I didn't say the questions was stupid. I said the thread was stupid.

How many times does the same thing have to be said before it's registered?

Gerard -- "The include statement is processed by the preprocessor so it can't be dynamically created in the executable..."
Dave Sinkula -- "The #include directive is like a copy-and-paste that happens at compile time. You can't do it at runtime."
AD -- "No because the #inc lude directive is processed at compile time, not runtime."
Me (Ironically) >> The #include thing only works on compile time. Not run time.

Then you finally say -- "The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???

Its undeniable, the point you make.

Be a part of the DaniWeb community

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