我使用minilibX库在C中制作光线跟踪引擎。 我希望能够在一个.conf
文件场景的配置,以读取显示:初始化char **的无限数量
例如:
([email protected] 117)cat universe.conf
#randomcomment
obj:eye:x:y:z
light:sun:100
light:moon:test
对象的数量可以在1至无限之间变化。 从现在起,我在一个char读取文件,拷贝每行1 1 **选项卡,并通过mallocing对象的数量发现,像这样:
void open_file(int fd, struct s_img *m)
{
int i;
char *s;
int curs_obj;
int curs_light;
i = 0;
curs_light = 0;
curs_obj = 0;
while (s = get_next_line(fd))
{
i = i + 1;
if (s[0] == 'l')
{
m->lights[curs_light] = s;
curs_light = curs_light + 1;
}
else if (s[0] == 'o')
{
m->objs[curs_obj] = s;
curs_obj = curs_obj + 1;
}
else if (s[0] != '#')
{
show_error(i, s);
stop_parsing(m);
}
}
现在,我想能够将每个tab[i]
的每个信息存储在新的char **tab
中,每个对象1个,使用':'
作为分隔。 所以我需要初始化和malloc
未定数char **tab
。我怎样才能做到这一点? (Ps:我希望我的代码和我的英文能够让你理解,而且我只使用基本的函数,比如读,写,打开,malloc ...等等,建立一切,如printf,get_line等)
根据需要分配新空间有什么问题? – atk
我需要为每个新对象分配一个新的char **选项卡。那么,我该怎么做? 有没有办法根据需要声明尽可能多的char **选项卡? – Antoninarto