delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/11/30/21:46:27

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Loading data in a single operation?!?
Date: 1 Dec 2001 02:36:44 GMT
Organization: Cornell University
Lines: 54
Sender: asu1 AT cornell DOT invalid (on 228.syracuse-02rh16rt.ny.dial-access.att.net)
Message-ID: <Xns9169DBD92A22EASINANUNUR@132.236.56.8>
References: <1tbg0uom8q1u46t3eo26cgqv261mahoet4 AT 4ax DOT com>
NNTP-Posting-Host: 228.syracuse-02rh16rt.ny.dial-access.att.net
X-Trace: news01.cit.cornell.edu 1007174204 21269 12.89.11.228 (1 Dec 2001 02:36:44 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 1 Dec 2001 02:36:44 GMT
User-Agent: Xnews/4.06.22
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


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