Link to home
Start Free TrialLog in
Avatar of sutejok
sutejok

asked on

#define supply from file?

I do understand how we can supply the value of an attribute from file,
but how to supply value of #define from file? since it's appears outside the function? where to extract a value from file we need to called a function , fopen , fscanf?

What I mean in here is I want to give the value of 1460 in MAXDATA from file instead inserting manually on the code

#define MAXDATA 1460

int main() {

MAXVAR = <value from file>

}

Illustration?
Avatar of Axter
Axter
Flag of United States of America image

Hi sutejok,
> >What I mean in here is I want to give the value of 1460 in MAXDATA from
> >file instead inserting manually on the code

Then make MAXDATA a global variable instead of a macro

David Maisonave :-)
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi sutejok,

Here is an example how to read/write the file. All points made above are valid comments !

include <stdio.h>

void main( void )
{
   FILE *stream;
   int  r, maxnum, w=20;

   /* This will write your file */
   if( (stream = fopen( "fread.out", "w" )) != NULL )
   {
      r= fwrite( &w, sizeof( int ), 1, stream );
      fclose( stream );
   }

   /* This will read your file */
   if( (stream = fopen( "fread.out", "r" )) != NULL )
   {
      r = fread( &maxnum, sizeof( int ), 1, stream );
      printf( "Maxnum = %d\n", maxnum );
      fclose( stream );
   }
   else
      printf( "File could not be opened\n" );

}

======
Werner
Redefine format of your file with values to contain name value pairs ... Something like
MAXDATA 1860

Run a precompilation script wihch inserts a #define in the beginning of each line and stores the resulting file in myheader.h

#include myheader.h

there are quite a few variations of the above method which can be used depending on your specific needs but the basic idea remains the same ... use a precompilation script to generate some part of the source code
If you are building your program from a shell script, you could try the following:

cc -DMAXDATA=`cat somefile`

You may have to change the cat command to a more advanced text processing
command if the file contains more than just the value for MAXDATA.