943,023 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3206
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2009
2

Dynamic include in C

Expand Post »
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

  1. {
  2. "john",
  3. 12,
  4. 68,
  5.  
  6. "steve",
  7. 12,
  8. 98,
  9.  
  10. "mat",
  11. 12,
  12. 95,
  13.  
  14. };

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

  1. struct std_
  2. {
  3. char name[50];
  4. int age;
  5. float avg;
  6. }
  7. std_str, *std_ptr;
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.  
  12. if(!strcmp(argv[2],"class1")
  13. {
  14. static std_str obj[200] =
  15. #include "class1.inc"
  16.  
  17. ...
  18. }
  19. else if(!strcmp(argv[2],"class2")
  20. {
  21. ...
  22. }
  23. return 0;
  24. }

The above code will work fine. But what I want is
  1. int main(int argc, char **argv)
  2. {
  3. char file[50];
  4.  
  5. /* I want to dynamically generate the include file name and include it */
  6. strcat(file, argv[2]);
  7. strcat(file, ".inc")
  8.  
  9. static std_str obj[200] =
  10. #include file
  11.  
  12. return 0;
  13. }

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

Is there anyway to achieve this?

Thanks and Regards,
Ahamed.
Similar Threads
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Nov 18th, 2009
0
Re: Dynamic include in C
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
Last edited by gerard4143; Nov 18th, 2009 at 1:35 pm.
Reputation Points: 499
Solved Threads: 363
Postaholic
gerard4143 is offline Offline
2,189 posts
since Jan 2008
Nov 18th, 2009
0
Re: Dynamic include in C
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 18th, 2009
-1
Re: Dynamic include in C
@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?
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Nov 18th, 2009
2
Re: Dynamic include in C
>>Is there anyway to achieve this?
No because the #inc lude directive is processed at compile time, not runtime.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,932 posts
since Aug 2005
Nov 18th, 2009
3
Re: Dynamic include in C
Click to Expand / Collapse  Quote originally posted by ahamed101 ...
@gerard4143 :
@Dave : The first example is working, the structure gets populated with values in the inc file.
Yes it does work.

Click to Expand / Collapse  Quote originally posted by ahamed101 ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,932 posts
since Aug 2005
Nov 18th, 2009
0
Re: Dynamic include in C
Click to Expand / Collapse  Quote originally posted by ahamed101 ...
@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
Reputation Points: 499
Solved Threads: 363
Postaholic
gerard4143 is offline Offline
2,189 posts
since Jan 2008
Nov 18th, 2009
2
Re: Dynamic include in C
Click to Expand / Collapse  Quote originally posted by ahamed101 ...
@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.
Last edited by Dave Sinkula; Nov 18th, 2009 at 1:54 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 18th, 2009
0
Re: Dynamic include in C
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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 18th, 2009
0
Re: Dynamic include in C
@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???
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help getting started.
Next Thread in C Forum Timeline: problem with strtok()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC