D
Debashish Chakravarty
Hi,
I have a file with a single column containing observations of an
experiment, the number of observations is not known in advance and I
have to read them all into an array, I am using realloc to keep on
changing the size of the array. My program is below. I would be
grateful for any comments.
Deb
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BFSIZ 1024
/*fname is the name of the file,on return nr contains the number of
observations, the function returns a pointer to the array containing
the observations if sucessful and NULL otherwise*/
double
*read_dbl_array(char *fname,int *nr)
{
size_t nd=32,/* space for doubles is allocated in multiples of nd
*/
nread=0,/*number of lines read so far*/
ninc=0;
FILE *fp;
double *d=NULL;
char buf[BFSIZ];
*nr=0;
fp=fopen(fname,"r");
if(NULL==fp)
{
perror(NULL);
return NULL;
}
while(fgets(buf,sizeof(buf),fp))
{
int i;
if(0==(nread%nd))/* time to allocate more memory*/
{
double *t;
t=realloc(d,(++ninc)*sizeof(*t)*nd);
if(NULL==t)
{
fclose(fp);
free(d);
return NULL;
}
d=t;
}
i=sscanf(buf,"%lf",d+(nread++));
if(i != 1)
{
fprintf(stderr,"invalid input at line %lu\n",(unsigned
long) nread);
fclose(fp);
free(d);
return NULL;
}
}
if(ferror(fp))
{
perror("error reading file");
fclose(fp);
free(d);
return NULL;
}
fclose(fp);
*nr=nread;
return d;
}
int
main(void)
{
char fname[FILENAME_MAX+1],*s;
double *d=NULL;
int nr=0;
printf("give file name\n");
fgets(fname,sizeof(fname),stdin);
if((s=strchr(fname,'\n')))
*s=0;
d=read_dbl_array(fname,&nr);
if(d)
{
printf("%d lines read\nFirst value:%f\tLast
value:%f",nr,d[0],d[nr-1]);
}
free(d);
return 0;
}
I have a file with a single column containing observations of an
experiment, the number of observations is not known in advance and I
have to read them all into an array, I am using realloc to keep on
changing the size of the array. My program is below. I would be
grateful for any comments.
Deb
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BFSIZ 1024
/*fname is the name of the file,on return nr contains the number of
observations, the function returns a pointer to the array containing
the observations if sucessful and NULL otherwise*/
double
*read_dbl_array(char *fname,int *nr)
{
size_t nd=32,/* space for doubles is allocated in multiples of nd
*/
nread=0,/*number of lines read so far*/
ninc=0;
FILE *fp;
double *d=NULL;
char buf[BFSIZ];
*nr=0;
fp=fopen(fname,"r");
if(NULL==fp)
{
perror(NULL);
return NULL;
}
while(fgets(buf,sizeof(buf),fp))
{
int i;
if(0==(nread%nd))/* time to allocate more memory*/
{
double *t;
t=realloc(d,(++ninc)*sizeof(*t)*nd);
if(NULL==t)
{
fclose(fp);
free(d);
return NULL;
}
d=t;
}
i=sscanf(buf,"%lf",d+(nread++));
if(i != 1)
{
fprintf(stderr,"invalid input at line %lu\n",(unsigned
long) nread);
fclose(fp);
free(d);
return NULL;
}
}
if(ferror(fp))
{
perror("error reading file");
fclose(fp);
free(d);
return NULL;
}
fclose(fp);
*nr=nread;
return d;
}
int
main(void)
{
char fname[FILENAME_MAX+1],*s;
double *d=NULL;
int nr=0;
printf("give file name\n");
fgets(fname,sizeof(fname),stdin);
if((s=strchr(fname,'\n')))
*s=0;
d=read_dbl_array(fname,&nr);
if(d)
{
printf("%d lines read\nFirst value:%f\tLast
value:%f",nr,d[0],d[nr-1]);
}
free(d);
return 0;
}