Hi guys, I've got a question that hopefully someone can answer.

I have a file that contains the following structure:

g hub
f 100 200 300 12
f 356 403 345 484
f 333 212 1203 393

g door glass
f 323 9292 383
f 32 3838 3939
f 1231 333 22
f 323 112 3939
f 323 3939 2281 2002 3883

[etc etc, there are 32 of these blocks]

Each block follows the same format, where 1 line starts with g followed by some string (all on one line) and then a bunch of lines that start with f followed by an unfixed number of integers.

My problem is I want to store the integers on these lines into a C integer array, so that I can pass it into an OpenGL function I am using.

My initial thought is for each block of data indicated by the string starting with g, I would count the number of lines for each object in the file, declare an array of that size, and then simply read in the numbers into the array.

Pretend each line that starts with f only had 3 numbers per line, I could go:

int array[NUM_LINES_OF_OBJECT][3];
char str[1024];
int i;
for(i = 0; i < NUM_LINES_OF_OBJECT; i++) {
      fscanf(myFile,"%s %f %f %f",str,&array[i][0],&array[i][1],&array[i][2]);
}

However there is an unknown number of integers that are on each line that begin with f. How can I make an array that has all the data for each line stored in it without knowing explicitly how many %f's to use in fscanf?

Ideally, I want to make 32 arrays for each of these "objects" from my text file.
int object1[Num_Object_Lines][ ?? ] <== unknown number of elements per line.

Does anyone have any idea how to do this? I would very much appreciate it if you could help me out.

Thanks.

Attached is the text file I am using. You might have to open it in an editor other than notepad otherwise the formatting will be screwed up.

Recommended Answers

All 6 Replies

Do you know the maximum number of values on a line? That would tell you how large to make the arrays of integers.

By the way, in your fscanf() , what does the %f do?

Use a good sized char array[100] or so, and fgets(array, sizeof(array), filePointer), to put one row of data into the char array.

Now, in a for loop from array[0] to array[strlen(array)], count up your spaces and add 1 - that is how many numbers you have in that row.

now malloc() memory for that size row, and sscanf() your numbers into the numbers array (which is a 2D dynamic array, of course, of type int).

The numbers array would be "ragged" in length on the high side, but save the maximum amount of memory, if that's what you want or need.

Personally, I'd use Walt's idea, simply because memory is plentiful. BTW, you could do this same type of thing with nodes, in a linked list.

commented: Thank you for your response. I appreciate it! +1

Oops, you're right Walt, I want to be using %d for my fscanf in this case. I was using %f for something else and forgot to change it.

And yes, I do know the maximum size of values on a line, it shouldn't be over 25.

However, I'm still not sure how I would go about implementing your idea given this knowledge, could you give me a tip? Thanks.

Also, Adak, that's a good idea and I might do that if there isn't a simpler way. :)

And yes, I do know the maximum size of values on a line, it shouldn't be over 25.

However, I'm still not sure how I would go about implementing your idea given this knowledge, could you give me a tip? Thanks.

Well, since you said

Pretend each line that starts with f only had 3 numbers per line,

maybe pretend they have 25.

commented: Very helpful in answering my questions! +1

Well, since you said

maybe pretend they have 25.

Hey, I solved my problem, probably a fairly tedious way. I created for each object a vector<vector<int > > that stores the numbers on each line into a vector<int> and then when the current line is done, I push that array into the vector<vector<int > >. Probably not the prettiest way.

However, out of curiousity regarding your above post, you're saying just pretend I have 25? Isn't the only reason that pretending I had 3 per line worked with fscanf because I had a constant format per line. There would always be 3 on each line, so it was easy enough to just go fscanf(myFile,"%d %d %d",&v1,&v2,&v3);

However, since I don't know the number of integers for each line, and only know that it COULD be up to 25, then how would I write an fscanf to do that? If I wrote %d 25 times inside it, wouldn't it continue to read the next line until it scanned 25 integers total?

Thanks

However, out of curiousity regarding your above post, you're saying just pretend I have 25? Isn't the only reason that pretending I had 3 per line worked with fscanf because I had a constant format per line. There would always be 3 on each line, so it was easy enough to just go fscanf(myFile,"%d %d %d",&v1,&v2,&v3);

However, since I don't know the number of integers for each line, and only know that it COULD be up to 25, then how would I write an fscanf to do that? If I wrote %d 25 times inside it, wouldn't it continue to read the next line until it scanned 25 integers total?

Thanks

Loop. Done when you read the last number on the line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.