简体   繁体   中英

How to read ints from a txt file and save it to different consts in C?

I found a couple of other posts that involve reading int's and outputting their values, however I want to know how to read int's and store them into consts.

I have defined some constants as such:

#define SEED 100
#define INIT_TIME 0
#define FIN_TIME 1000
#define ARRIVE_MIN 1
#define ARRIVE_MAX 5
#define QUIT_PROB 0.2
#define CPU_MIN 1
#define CPU_MAX 5
#define DISK1_MIN 1
#define DISK1_MAX 5
#define DISK2_MIN 1
#define DISK2_MAX 5

First I would like to know if there is a way to have all these constants saved to a .txt file and just read/use it.

Otherwise, how would I store each their values from the text file to itself?

100
0
1000
1
5
0.2
1
5
1
5
1
5 

For example, have the first number (100) stored to SEED then 0 stored to INIT_TIME, etc.

I have tried some code from another stack post that I believe allows you to store values into variables in one line but I get an infinite loop of 0s. This is the code I tried:

fscanf("index.txt", "%d %d %d %d %d %d %d %d %d %d %d %d”, &SEED, &INIT_TIME,&FIN_TIME,&ARRIVE_MIN,&ARRIVE_MAX, &QUIT_PROB,&CPU_MIN,&CPU_MAX,&DISK1_MIN,&DISK1_MAX,&DISK2_MIN,&DISK2_MAX)

You can not change the value of a macro. Before a piece of source code is processed by a compiler, all instances of SEED , for example, in your source code will be replaced with 100 . They are not variables that can be changed; they don't even exist anymore after your code compiles. They are simply placeholders that are filled in before your code is compiled.

If you really want to store these macros in a separate file, you can simply store them all in a constants.h file and in your code use #include "constants.h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM