delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/11/30/20:31:38

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: Radical DOT NetSurfer AT delorie DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: Loading data in a single operation?!?
Date: Fri, 30 Nov 2001 20:18:51 -0500
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <1tbg0uom8q1u46t3eo26cgqv261mahoet4@4ax.com>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
X-Complaints-To: newsabuse AT supernews DOT com
Lines: 54
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie 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);
       }
3- - - - - - - - - - - - - - - - -
         if (((char*)StringArray[i] = (char*)malloc(13))==NULL) {
          printf("Have reached the limit for the StringArray!.\n");
          break;
          }
         memset(StringArray[i], 0, 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()   ???

the string array in memory has the actual format
of:
char *StringArray1[i] = {
  "item1a", "item1b",
 "item2a", "item2b",
 etc.  etc.
};

the sizes of each string is unique....
the size of the array may change the next time
the program runs,
any ideas?

THANKS!

email: radsmail AT juno DOT com
http://members.tripod.com/~RadSurfer/


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019