Hi all,
I'm writing a program to read some experimental data from a file, then do some manipulations, and then to write the new data into another file. The program works ok, but after a few datapoints, the program does not read values and prints zeros instead.
For a single reading, I have the format of "date time value", like the example below:
Code:
06/12/2007 04:45:14 142.74
This is repeated 8 times in a single line (for the 8 different types of measurements), and grows up in rows as the experiment carries on.
I want to read the broken down date, convert it to seconds since the first data point entry (so as to show the time in secs when the reading was taken), and then print this, along with the actual reading.
Valgrind shows errors coming from two lines in my program but cannot see what is wrong.... Maybe I'm not allocating correctly memory for my structure, but then again, there's no printed statements due to NULL pointers when I check for them...
Here's my code, it's very simplified in order to reduce the amount of lines I'm posting.
Code:
#include<stdio.h> /* for input and output */
#include<stdlib.h> /* for memory management */
#include<string.h> /* for memset */
#include<time.h> /* for time related functions */
struct dpnt {
struct tm timh;
double value;
};
int main(void) {
int year=0; /* dummy year integer */
int month=0; /* dummy month variable */
unsigned int nrows=0; /* rows in data file */
const unsigned int ncols=8; /* number of forces being measured */
unsigned int i=0, j=0; /* counters */
struct dpnt **data=NULL; /* data structure, ie time-value datasets */
FILE *fp=NULL; /* pointer to file to read data */
FILE *newfp=NULL; /* pointer to file to write data */
char tempbuff[21]; /* temporary string */
char templine[700]; /* temporary line */
/* open files */
fp=fopen("few.orig.data.dat","r");
newfp=fopen("test","w");
/* count the number of data lines */
while (fgets(templine,sizeof(templine),fp)!=NULL) {
nrows++;
}
/* allocate 2d structure array */
data=malloc(nrows*sizeof(*data));
if (data==NULL) {
printf("\nCould not allocate array of pointers");
exit(1);
}
for (i=0; i<nrows; i++) {
data[i]=malloc(ncols*sizeof(*data[i]));
if (data[i]==NULL) {
printf("\nCould not allocate structure array data[%u]", i);
exit(1);
}
}
/* import data from file into appropriate storage types */
rewind(fp);
for (i=0; i<nrows; i++) {
for (j=0; j<ncols; j++) {
fscanf(fp,"%i%*c%i%*c%i %i%*c%i%*c%i %lf", &data[i][j].timh.tm_mday, &month,
&year, &data[i][j].timh.tm_hour, &data[i][j].timh.tm_min,
&data[i][j].timh.tm_sec, &data[i][j].value);
data[i][j].timh.tm_mon=month-1;
data[i][j].timh.tm_year=year-1900;
}
}
/* write data into new file */
for (i=0; i<nrows; i++) {
for (j=0; j<ncols; j++) {
memset(tempbuff, '\0', sizeof(tempbuff));
strftime(tempbuff, 20, "%d/%m/%Y %H:%M:%S", &data[i][j].timh);
fprintf(newfp, "%s %010.0f %08.2f\t", tempbuff,
difftime(mktime(&data[i][j].timh),mktime(&data[0][j].timh)),
data[i][j].value);
}
fprintf(newfp, "\n");
}
/* clean up memory */
for (i=0; i<nrows; i++) {
free(data[i]);
}
free(data);
fclose(fp);
fclose(newfp);
return 0;
}
Valgrind shows the folloing errors for the lines below:
Code:
"Conditional jump or move depends on uninitialised value(s)"
strftime(tempbuff, 20, "%d/%m/%Y %H:%M:%S", &data[i][j].timh);
Code:
"Conditional jump or move depends on uninitialised value(s)"
"Use of uninitialised value of size 4"
"Syscall param write(buf) points to uninitialised byte(s)"
"Address 0x400AC5E is not stack'd, malloc'd or (recently) free'd"
fprintf(newfp, "%s %010.0f %08.2f\t", tempbuff,
difftime(mktime(&data[i][j].timh),mktime(&data[0][j].timh)),
data[i][j].value);
Can anyone see what my mistake is?
Thanks a lot!
Spiros