?
Solved

#define supply from file?

Posted on 2005-03-16
6
Medium Priority
?
192 Views
Last Modified: 2010-04-15
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
Comment
Question by:sutejok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13559475
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
 
LVL 30

Accepted Solution

by:
Axter earned 1200 total points
ID: 13559486
A #define macro is a constant, and can not be changed durring runtime.

It's value is determined at compile time.
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 800 total points
ID: 13559675
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:griessh
ID: 13560042
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13561355
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 13562719
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question