X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f From: Radical DOT NetSurfer AT unknown DOT addr Newsgroups: comp.os.msdos.djgpp Subject: Re: Loading data in a single operation?!? Date: Mon, 03 Dec 2001 17:13:09 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <1tbg0uom8q1u46t3eo26cgqv261mahoet4 AT 4ax DOT com> <3C0B7127 DOT 5080F19F AT eik DOT bme DOT hu> X-Newsreader: Forte Agent 1.8/32.548 MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: newsabuse AT supernews DOT com Lines: 68 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I wish to thank you for the time you took to post this. Nice work. Thanks. //RadSurfer// I just need to put "trust" into using this new approach... it was easy using embedded strings, just not very convenient to switch between data without re-compiling using a different name or something... at least this approach leaves me with a single program, and hopefully an endless supply of data files... I also note that your approach did not use strtok that someone else suggested... fascinating... your code is the winner I believe ;-) On Mon, 03 Dec 2001 13:33:43 +0100, "Dr.András Sólyom" wrote: >Radical DOT NetSurfer AT delorie DOT com wrote: > >> 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: >> >> > >.. code snippet cut > >> >> 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? >> > >Store 0 terminated strings in the file! You than simple read in the the >whole file with fread after >allocating enough space for it (memory size will be 1 more than file >size,determined by stat/fstat). Put a 0 into the last byte of the >allocated buffer. Then run through these strings in memory to get the >number of strings in it and allocate and fill up your array. You can >determine where the array ends utililising the additional 0 you have put >there: if the next string is empty then you have reached the end. Like: > char *p = buffer; int count = 0; int i = 0; > while(*p) { ++count; p = p + strlen(p)+1; } > StringArray = (char**) calloc(count, sizeof(char*) ); > while(*p) { StringArray[i] = p; ++i; p = p + strlen(p)+1; } > > Andras