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.

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

Edited 7 Years Ago by gerard4143: n/a

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.

@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?

@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.

@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

@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.

Edited 7 Years Ago by Dave Sinkula: n/a

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.

@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???

@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.

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.

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.

Comments
I agree

>> 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???

Edited 7 Years Ago by twomers: n/a

>> 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.

This question has already been answered. Start a new discussion instead.