help!! problem with arrays and reading from a file

Posted By: Darkmax

help!! problem with arrays and reading from a file - 02/10/10 18:10

sorry for my bad english
i´m trying to make a application to read from a text file like this:

No. Models: 3

1: soldier.mdl
id: 5

2: tank.mdl
id: 3

3: gun.mdl
id: 9

But also could be another quantity of models.

once the program read the file i want to create two arrays of strings with the size of number of models(i.e. two arrays of 3 strings for the last text file) to save in one the name of the model and in the other one the id of each model.

for example the final result will be:

array models
0 - solider.mdl
1 - tank.mdl
2 - gun.mdl

array of ids
0 - 5
1 - 3
2 - 9

i can do this very easy in c++ but in lite-c i´m having a lot of troubles.

if someone can give me some tips to do this?
Posted By: KDuke

Re: help!! problem with arrays and reading from a file - 02/10/10 18:29

How about structs? That would be somewhat handier than having two (or maybe later on more) single arrays.

Here's some example:

Code:
struct MyWhatever {
  STRING* modelname;
  int id;
} MyWhatever;

MyWhatever MyArray[50];

MyArray[0].modelname = str_create("Hello World");
MyArray[0].id = 2;



Hope this helps you.

greetings
K-Duke
Posted By: Joey

Re: help!! problem with arrays and reading from a file - 02/10/10 18:30

if you can do this very easy in c++ then it should be no problem for you to rewrite it in plain c.

look up file_open_read, file_str_readto, file_var_read. this should give you the input. and instead of using the new[] and delete[] operators for arrays (or a std::list), have a look at one of the dynamic array implementations in the lite-c contributions forum (e.g. i have written one some time ago). you can allocate strings with str_create and (single) vars with
var* newvar = (var*)malloc(sizeof(var));

joey.
© 2023 lite-C Forums