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.

Attachments
g fuselage
f 49  50  51  52 
f 53  54  55 
f 56  57  58 
f 59  60  61  62 
f 63  64  65  66 
f 67  68  69  70 
f 70  71  67 
f 72  73  74 
f 75  70  76 
f 75  71  70 
f 77  78  79 
f 80  81  82 
f 82  83  80 
f 84  85  86  87 
f 88  89  90  91 
f 92  93  94  95 
f 96  97  98 
f 97  99  98 
f 100  101  102  103 
f 104  105  106  107 
f 108  109  110  111 
f 112  113  114  115 
f 52  51  116  117 
f 118  119  120  121 
f 122  123  124  125 
f 126  127  128  129 
f 130  131  132  133 
f 134  135  136  137 
f 138  139  140  141 
f 142  143  144  145 
f 146  147  148  149 
f 150  151  152  153 
f 154  155  156  157 
f 158  159  160  161 
f 162  163  164  165 
f 84  166  167  85 
f 168  169  170  171 
f 172  54  173  174 
f 175  176  177  178 
f 179  180  181  182 
f 90  183  184  91 
f 129  185  126 
f 135  186  136 
f 143  187  144 
f 67  148  147 
f 147  68  67 
f 157  188  154 
f 189  190  165 
f 190  162  165 
f 166  191  167 
f 192  151  150  193 
f 140  194  195  141 
f 196  197  192  193 
f 198  129  128  199 
f 200  201  196  202 
f 203  204  185  129  198 
f 205  206  201  200 
f 207  208  204  203 
f 209  210  206  205 
f 211  69  212  195 
f 211  213  69 
f 67  214  215  216  148 
f 217  218  219  220 
f 221  222  223  224 
f 225  226  218  217 
f 227  228  229  230  231  232 
f 233  234  235  236 
f 236  237  238  233 
f 72  239  240  241 
f 242  151  192  243  212 
f 197  244  198  199 
f 245  246  207  203 
f 213  247  69 
f 248  249  225  228 
f 250  251  233 
f 233  252  234 
f 233  251  252 
f 216  215  253  229 
f 220  149  148  216 
f 254  255  256  257 
f 112  258  259  113 
f 260  261  59  62 
f 262  263  264  255 
f 265  266  254  267 
f 108  268  269  109 
f 270  271  272  273 
f 274  275  263  262 
f 269  268  276  266  265 
f 277  278  279  280 
f 281  282  278  277 
f 283  284  285  286 
f 287  288  289  290  291  292 
f 293  294  295  282 
f 296  297  281  298 
f 299  300  301  302 
f 303  304  293  297 
f 305  306  307  304 
f 308  309  303  310 
f 311  312  102  107  313 
f 314  315  300  299 
f 64  316  306  305 
f 317  65  309  308 
f 318  103  102  312 
f 319  320  315  314 
f 321  322  320  319 
f 64  63  323 
f 97  324  317  100 
f 322  325  318  320 
f 326  99  325 
f 326  98  99 
f 327  328  322  321 
f 329  330  63  66 
f 331  332  333 
f 333  334  331 
f 335  336  326 
f 336  98  326 
f 337  338  328  327 
f 107  106  313 
f 271  339  340  341  342  272 
f 343  344  268  108 
f 280  279  274  276 
f 345  249  256  346 
f 248  347  348  259  257 
f 349  350  351  114 
f 342  352  353  273  272 
f 354  355  351  59  261 
f 329  66  356 
f 357  358  359 
f 360  361  362 
f 124  363  364  125 
f 365  366  367  368 
f 369  370  371  372 
f 373  374  375  376  377 
f 378  379  380  381 
f 92  382  93 
f 383  384  385  386 
f 387  388  82 
f 82  389  387 
f 360  390  391  392  361 
f 393  392  389 
f 393  394  392 
f 395  362  396  397 
f 386  385  398  399 
f 389  81  393 
f 389  82  81 
f 80  399  166  84 
f 359  393  169  168 
f 397  55  54  172 
f 191  400  401  402 
f 403  184  402  401  404  405 
f 181  88  95  406 
f 177  182  407  408 
f 407  406  409  410  411 
f 412  408  413  414 
f 405  404  415  416 
f 411  410  417  418 
f 414  413  419  420 
f 421  422  423  424  425  426 
f 427  428  429  430 
f 419  418  431  432 
f 426  425  57  433 
f 432  431  373  370 
f 434  435  436  437 
f 438  121  120  439 
f 365  440  441  442 
f 443  366  444  445 
f 446  447  448 
f 449  450  451  452 
f 453  454  455  456 
f 457  458  163  459  460 
f 461  462  463 
f 461  464  465  466  462 
f 467  436  442  441  438  439 
f 445  444  435  468 
f 469  470  471  466  465 
f 467  472  436 
f 473  190  189  437 
f 473  474  190 
f 447  475  476  459 
f 447  446  475 
f 449  477  478  450 
f 479  480  481  482 
f 450  478  483  453 
f 482  481  131  130 
f 453  483  484  454 
f 134  455  460  135 
f 194  140  186  476 
f 458  457  485  156 
f 156  485  157 
f 485  486  157 
f 476  186  135  460  459 
f 484  487  486  485 
f 454  484  485  457 
f 488  456  455  134 
f 483  489  487  484 
f 131  481  451  456  488 
f 478  490  489  483 
f 480  452  451  481 
f 477  491  490  478 
f 475  492  194  476 
f 493  434  159  158 
f 494  448  447 
f 468  435  434  493 
f 495  494  496  79 
f 496  77  79 
f 444  442  436  435 
f 466  471  497 
f 116  498  499  117 
f 462  500  50 
f 50  49  462 
f 500  462  466  497 
f 447  459  163  162 
f 454  457  460  455 
f 450  453  456  451 
f 190  496  494 
f 494  447  162  190 
f 366  365  442  444 
f 501  119  118  502 
f 501  464  503 
f 464  461  503 
f 473  437  436  472 
f 159  434  437  189 
f 58  57  504  505 
f 506  432  370  369 
f 431  507  374  373 
f 425  508  504  57 
f 420  419  432  506 
f 418  417  427  430  507  431 
f 509  422  421  510 
f 424  511  508  425 
f 415  512  511  424 
f 413  411  418  419 
f 416  415  424  423 
f 404  513  512  415 
f 408  407  411  413 
f 178  177  408  412 
f 182  181  406  407 
f 91  184  403  382  92 
f 401  514  513  404 
f 400  515  514  401 
f 398  516  515  400 
f 54  53  359  168 
f 393  81  87  169 
f 166  399  398  400  191 
f 83  399  80 
f 83  386  399 
f 385  517  516  398 
f 518  357  359  53 
f 519  390  360  362  395 
f 391  387  389 
f 389  392  391 
f 520  386  83 
f 520  383  386 
f 384  521  517  385 
f 406  95  94  409 
f 379  433  57  56  380 
f 375  522  523  376 
f 370  373  377  371 
f 366  443  524  367 
f 363  440  365  368  364 
f 525  123  122  526 
f 361  392  394  527 
f 53  55  396  518 
f 323  63  330  528 
f 353  354  261  273 
f 351  350  60  59 
f 348  529  113  259 
f 249  248  257  256 
f 279  530  275  274 
f 344  531  280  276  268 
f 341  532  111  342 
f 104  310  533  105 
f 338  335  326  328 
f 534  334  96 
f 334  333  96 
f 535  356  66 
f 66  324  535 
f 328  326  325  322 
f 96  324  97 
f 96  333  324 
f 325  99  103  318 
f 324  66  65  317 
f 323  536  316  64 
f 320  318  312  315 
f 100  317  308  101 
f 65  64  305  309 
f 315  312  311  537  300 
f 101  308  310  104 
f 309  305  304  303 
f 533  310  303  297  296 
f 304  307  294  293 
f 300  537  538  301 
f 297  293  282  281 
f 302  301  287  539 
f 539  287  292  540 
f 289  541  542  290 
f 285  284  298  281  277  543 
f 282  295  544  278 
f 540  292  339  545 
f 543  277  280  531 
f 278  544  530  279 
f 276  274  262  266 
f 545  339  271  270  546 
f 342  111  110  352 
f 260  270  273  261 
f 266  262  255  254 
f 547  546  270  260  548 
f 355  115  114  351 
f 258  267  254  257  259 
f 255  264  346  256 
f 217  220  216  229 
f 549  232  231  550 
f 347  248  228  227 
f 249  345  226  225 
f 71  214  67 
f 244  245  203  198 
f 192  197  199  243 
f 212  69  68  242 
f 239  551  552  240 
f 236  72  241  237 
f 553  73  554  555 
f 224  556  557  221 
f 558  559  250  233  238 
f 228  225  217  229 
f 560  561  558  238 
f 471  470  560  238  237 
f 230  229  253 
f 70  69  247 
f 220  219  562  149 
f 492  211  195  194 
f 210  246  245  206 
f 208  479  482  204 
f 206  245  244  201 
f 204  482  130  185 
f 201  244  197  196 
f 199  128  563  243 
f 202  196  193  564 
f 563  141  195  212  243 
f 193  150  564 
f 150  187  564 
f 548  565  566  547 
f 470  469  560 
f 402  184  183 
f 163  458  164 
f 567  158  161 
f 151  242  152 
f 562  142  145 
f 141  563  138 
f 131  488  132 
f 180  89  88  181 
f 176  179  182  177 
f 54  168  171  173 
f 169  87  86  170 
f 191  402  183  167 
f 159  189  165  160 
f 161  154  188  567 
f 155  164  458  156 
f 147  152  242  68 
f 145  146  149  562 
f 187  150  153  144 
f 186  140  139  136 
f 488  134  137  132 
f 133  126  185  130 
f 127  138  563  128 
f 557  556  553  555 
f 549  550  568  569 
f 113  529  349  114 
f 343  108  111  532 
f 283  286  542  541 
f 101  104  107  102 
f 97  100  103  99 
f 534  98  336 
f 534  96  98 
f 378  381  523  522 
f 509  510  429  428 
f 91  92  95  88 
f 80  84  87  81 
f 83  82  388 
f 388  520  83 
f 78  499  498  79 
f 75  76  223 
f 223  222  75 
f 73  72  236 
f 236  554  73 
f 251  250  569  568 
f 394  359  358 
f 394  393  359 
f 496  190  474 
f 50  500  570 
f 528  536  323 
f 396  55  397 
f 619  571  572 
f 573  574  575  576  577  578  579  610  609  623  612  611  614  613 
f 573  613  622  616  615  621  618  617  620  390  519  580  581 
f 501  502  464 
f 58  505  582 
f 583  561  584 
f 585  586  626  587 
f 565  588  585  587  566 
f 471  240  552 
f 240  471  241 
f 551  589  590  552 
f 61  60  559  558 
f 292  291  340  339 
f 591  592  593 
f 582  505  626  586  594 
f 593  594  586  585  588 
f 591  595  583  584  596 
f 589  597  598  590 
f 599  500  497  600 
f 600  497  471 
f 471  598  600 
f 584  469  465  596 
f 471  590  598 
f 502  601  596  465  464 
f 597  599  600  598 
f 602  591  596  601 
f 588  565  548  260  62  61  595 
f 583  595  61  558  561 
f 237  241  471 
f 471  552  590 
f 592  591  602  525  526 
f 593  588  595  591 
f 603  579  578  604  605 
f 332  606  607  535 
f 603  605  606 
f 331  579  603  606  332 
f 605  604  536  528 
f 535  333  332 
f 535  324  333 
f 301  538  288  287 
f 624  625  608 

g access5
f 627  628  629  630  631 
f 632  633  634  635 
f 636  637  638  633  632 
f 639  640  637  636 
f 641  642  629  628 
f 631  630  635  634 

g fuslage2
f 643  644  645 
f 646  647  648 
f 649  650  651 
f 652  653  654 
f 655  656  657  658 
f 659  660  661  657 
f 662  663  664  665 
f 666  667  668  669 
f 670  671  672  673 
f 674  675  676  677 
f 678  679  680  681 
f 682  683  684  685 
f 686  687  688  689 
f 657  656  659 
f 663  690  664 
f 691  668  667 
f 692  680  679 
f 693  688  687 
f 694  695  696  697  698 
f 699  700  694  698 
f 701  702  703  675 
f 704  705  699  702 
f 706  707  701  708 
f 709  710  705  704 
f 711  712  707  706 
f 713  714  710  709 
f 715  716  712  711 
f 717  658  665  664 
f 718  671  697 
f 718  691  671 
f 719  720  653  669  668 
f 721  722  650  718  723 
f 650  724  725 
f 726  727  728  729  653 
f 730  731  732  728  727 
f 733  734  732  731 
f 735  736  737  738 
f 739  740  734  733 
f 741  742  740  739 
f 743  744  742  741 
f 745  746  747  748  749  750 
f 751  752  744  743 
f 753  754  747  746 
f 755  756

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.

Comments
Thank you for your response. I appreciate it!

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. :)

Edited 5 Years Ago by t_man700: n/a

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.

Edited 5 Years Ago by WaltP: n/a

Comments
Very helpful in answering my questions!

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

Edited 5 Years Ago by t_man700: n/a

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.

This article has been dead for over six months. Start a new discussion instead.