简体   繁体   English

从文件读取数组大小

[英]Reading array size from file

I have a program which should adjust number of elements to number of devices it is working with. 我有一个程序,可以根据要使用的设备数来调整元素数量。 I have a config *.txt file that contains some parameters that allows users who don't know programming language to adjust program to their needs. 我有一个config * .txt文件,其中包含一些参数,这些参数使不懂编程语言的用户可以根据自己的需要调整程序。

For example till now everything have been handled like this. 例如,到目前为止,一切都已像这样处理。 In header file: 在头文件中:

enum 
{
    // number of input and output channels
    kMaxInputChannels = 8,
    kMaxOutputChannels = 8
};

typedef struct AudioDriverSettings
{
   (...)
   ASIOBufferInfo bufferInfos[kMaxInputChannels + kMaxOutputChannels]; 
   ASIOChannelInfo channelInfos[kMaxInputChannels + kMaxOutputChannels];
   (...)

} AudioDriverSettings;

typedef struct AudioFileConfig
{
   (...)
   int inputId[kMaxInputChannels];
   int outputId[kMaxOutputChannels];
   bool shouldMixInput[kMaxInputChannels];
   bool shouldRecordChannel[kMaxInputChannels];
   (...)

} AudioFileConfig;

In *.txt there are variables: * .txt中有变量:

NUM_CHANNELS_IN             8
NUM_CHANNELS_OUT            8

And on program start I am reading it and writing to variable: 在程序启动时,我正在读取它并写入变量:

if (!strcmp(tmp_str, "NUM_CHANNELS_IN")) 
        NUM_CHANNELS_IN = atoi(token);
if (!strcmp(tmp_str, "NUM_CHANNELS_OUT")) 
        NUM_CHANNELS_OUT = atoi(token);

I would like to get effect as below but variable needs to be const so it isn't working. 我想获得如下效果,但是变量需要为const,所以它不起作用。

int NUM_CHANNELS_IN;
int NUM_CHANNELS_OUT;

typedef struct AudioDriverSettings
{
   (...)
   ASIOBufferInfo bufferInfos[NUM_CHANNELS_IN + NUM_CHANNELS_OUT]; 
   ASIOChannelInfo channelInfos[NUM_CHANNELS_IN + NUM_CHANNELS_OUT];
   (...)

} AudioDriverSettings;

typedef struct AudioFileConfig
{
   (...)
   int inputId[NUM_CHANNELS_IN];
   int outputId[NUM_CHANNELS_OUT];
   bool shouldMixInput[NUM_CHANNELS_IN];
   bool shouldRecordChannel[NUM_CHANNELS_IN];
   (...)

} AudioFileConfig;

Is there any simple way to handle it? 有没有简单的处理方法?

If this is C, you need to allocate your arrays dynamically: 如果是C,则需要动态分配数组:

ASIOBufferInfo *bufferInfos;
...
bufferInfos = malloc(sizeof(ASIOBufferInfo) * (NUM_CHANNELS_IN + NUM_CHANNELS_OUT));

If this is C++, use the std::vector class: 如果是C ++,请使用std::vector类:

std::vector<ASIOBufferInfo> bufferInfos;
...
bufferInfos.reserve(NUM_CHANNELS_IN + NUM_CHANNELS_OUT);

and then push_back to the vector . 然后push_backvector Or: 要么:

std::vector<ASIOBufferInfo> bufferInfos(NUM_CHANNELS_IN + NUM_CHANNELS_OUT);

and then just access the elements like bufferInfos.at(i) . 然后只需访问诸如bufferInfos.at(i)类的元素。

I Believe the answer you are looking for is to use pointers. 我相信您正在寻找的答案是使用指针。 By changing from: 通过更改:

int NUM_CHANNELS_IN; int NUM_CHANNELS_IN; int NUM_CHANNELS_OUT; int NUM_CHANNELS_OUT;

to: int *NUM_CHANNELS_IN; 到:int * NUM_CHANNELS_IN; int *NUM_CHANNELS_OUT; int * NUM_CHANNELS_OUT;

You will be able to pass your variable out of functions etc. I cannot say the exact changes you will need to make to your code, you may need to brush up on the proper syntax for pointers, But i believe this is the simplest way to achieve what you are trying to do if I have correctly interpreted your question. 您将能够从函数等中传递变量。我不能说您需要对代码进行确切的更改,您可能需要重新编写指针的正确语法,但是我相信这是最简单的方法如果我正确解释了您的问题,请实现您想要做的事情。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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