Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading data into memory 1

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
hi,

I am writing a program in which I have to read two columns products, price by querying the database and read it into memory. Instaed of querying the database everytime I can load the required data into memory for faster access. I am wondering how I would be able to acheive this i.e. what data structure will i need to use. Any help would be appreciated.

Thanks
 
hi,

struct myrecord
{
char name[25];
int age;
};

struct myrecord memdb[MAX];

...

opendb
select or other

int i=0;
while(TRUE)
{
rc = read( rec, dbhandle ); // prototype
if( rc==EOF ) // pro.
break ;

memcpy( memdb.name, rec.Name, sizeof(??) );
memdb.age = rec.Age ;
i++ ;
if( i>=MAX )
{
Signal( "MAX" ) ;
break
}
}

// i contains n elem

closedb


bye
 

victor's way is correct, but in my eyes the 'MAX' is a big limit
don't limit your program to a MAX-value, very hard to debug.
do it dynamically, usind alloc,realloc
now the installed physical-mem of the system, is your limit.

put this in a file called 'array.h'

#define ValueLF __LINE__,__FILE__
#define DeclaLF int ccline, char *ccfile
#define ParamLF ccline, ccfile
#define PrintLF "\n[%d]%s: Fatal-Error calling %s Out of Memory. Exiting\n\n",ParamLF

struct xxx { long cnt; char **line; };
typedef struct xxx MyArray;

int dejavu_arr(MyArray *,char *);
int freearray(MyArray *);
int showarray(MyArray *);
void add2array(MyArray *,char *,int,char *);
MyArray *newarray(MyArray *);


------------- this is your prog

#include <stdio.h>
#include .....
#include .....
#include .....
#include .....
#include &quot;array.h&quot;

int main(int argc, char **argv)
{
char *ck_malloc(unsigned int,int,char *);
char *ck_realloc(char *,unsigned int,int,char *);

MyArray *newarray(MyArray *);
MyArray *array = (MyArray *)NULL;

array = newarray(array); /* initialize it */

while( .....reading somethings in buff .....){
if(dejavu_arr(array,buff)) continue; /* to unique fill array */
add2array(array,buff,ValueLF);
}
showarray(array);
freearray(array);
exit(0);
}

#define LINEA array->line
#define INDEX array->cnt

MyArray *newarray(MyArray *array)
{
if(array == NULL) array = (MyArray *)calloc(1,(unsigned)sizeof(MyArray));
if(array == NULL) exit(fprintf(stderr,&quot;cannot alloc struct, exiting\n&quot;));
INDEX = (long)NULL; LINEA = (char **)NULL; /* init array to NULL */
return(array);
}

int showarray(MyArray *array)
{
if(INDEX){
int pos;
for(pos = 0; INDEX >pos; ++pos) printf(&quot;%s\n&quot;,LINEA[pos]);
}
return(INDEX);
}

int freearray(MyArray *array)
{
while(INDEX >0) free(LINEA[--INDEX]);
free(LINEA);
return(0);
}

int dejavu_arr(MyArray *array, char *toadd)
{
if(toadd){
int pos = 0;
for(; INDEX >pos; ++pos) if(!strcmp(toadd,LINEA[pos])) return(1+pos);
}
return(0);
}

void add2array(MyArray *array, char *toadd, DeclaLF)
{
if(!toadd) return;
if(!INDEX++) LINEA = (char **)ck_malloc((INDEX * sizeof(char *)),ParamLF);
else LINEA = (char **)ck_realloc((char *)LINEA,(INDEX * sizeof(char *)),ParamLF);
LINEA[INDEX-1] = (char *)ck_malloc(strlen(toadd),ParamLF);
strcpy(LINEA[INDEX-1],toadd);
return;
}

char *ck_malloc(unsigned int size, DeclaLF)
{
register char *pmem;
if((pmem = malloc(size))) return(pmem);
exit(fprintf(stderr,PrintLF,&quot;ck_malloc()&quot;));
}

char *ck_realloc(char *pmem, unsigned int size, DeclaLF)
{
if((pmem = realloc(pmem,size))) return(pmem);
exit(fprintf(stderr,PrintLF,&quot;ck_realloc()&quot;));
}
vox clamantis in deserto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top