• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

#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?
0
sutejok
Asked:
sutejok
2 Solutions
 
AxterCommented:
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!
0
 
AxterCommented:
A #define macro is a constant, and can not be changed durring runtime.

It's value is determined at compile time.
0
 
grg99Commented:
it's a bit tricky.   thesrres some constants that HAVE to be defined at compile time.

Array sizes being one of those.

So as you've found out, you can't read MAXDATA from a file and then say


double   HoldsTheData[ MAXDATA ];

at least not in C.

what you can do is make a blind pointer:

double * HoldsTheData[];

Then when you finally have the value read in, you can allocate an array with malloc:

HoldsTheData = (double *) malloc( MAXDATA * sizeof( MAXDATA );

Now you have a pointer to the right size array.   Now you can use HoldsTheData as usual.

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
griesshCommented:
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
0
 
sunnycoderCommented:
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
0
 
brettmjohnsonCommented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now