Mail Archives: djgpp/2001/11/30/21:46:27
Radical NetSurfer wrote in news:1tbg0uom8q1u46t3eo26cgqv261mahoet4 AT 4ax DOT com:
> Here's a toughy:
>
> Using conventional C-style code,
> something I wish to be portable,
> how would you load in data for a
> two-dimensional string array...
> dynamically allocated:
>
> example:
> 1- - - - - - - - - - - - - - - - -
> unsigned char **StringArray;
> 2- - - - - - - - - - - - - - - - -
> if ( (StringArray = (char **)malloc(1024*sizeof(char*)))==NULL ) {
> printf("Error allocating buffer.\n");
> exit(-1);
> }
in C, you do not need to cast the return value of malloc, and it is not
considered good style to do so. also, exiting with -1 does not really mean
anything. you should use EXIT_FAILURE (defined in stdlib.h) in the absence
of any reason to prefer otherwise.
> 3- - - - - - - - - - - - - - - - -
> if (((char*)StringArray[i] = (char*)malloc(13))==NULL) {
> printf("Have reached the limit for the StringArray!.\n");
> break;
> }
> memset(StringArray[i], 0, 13);
where are you getting the magic value of 13?
> strcpy(StringArray[i], data);
> ++i;
> - - - - - - - - - - - - - - - - -
> NOW, here's the really tricky part!
>
> in #3 I am getting data currently from a file,
> reading a line at a time, and feeding that into
> the freshly allocated string_array[i],
>
> HOW CAN I SIMPLY LOAD THE _entire_ FILE
> INTO MEMORY in one swift operation ?
> say, using fread() ???
i would say write your own flexible parser which creates, populates, and
returns the data structure you want to get.
sinan.
--
--------------------------------
A. Sinan Unur
http://www.unur.com/
- Raw text -