If I write to the array as below
and print of the results in PCR1[i*pagesize] I get a only arr[3] values. When I take out the comments, the PCR1[i*pagesize] has other values.
The entire code is here
Code:
if((d == 1) && (t == 0))
{
sprintf(&PCR1[no*pagesize], "%s",arr[3]);
no++;
/* if (strcmp(arr[6],"P") == 0)
{
amount = atoi(arr[19]);
sprintf(&PCR1[(no++)*pagesize + d*rowsize + t*element], "%d", amount);
}
else
{
amount = atoi(arr[19]);
sprintf(&PCR1[(no++)*pagesize + (d+1)*rowsize * t*element], "%d", amount);
}
*/
and print of the results in PCR1[i*pagesize] I get a only arr[3] values. When I take out the comments, the PCR1[i*pagesize] has other values.
The entire code is here
Code:
#define dates 3
#define times 3
#define time1 "0940"
#define time2 "1430"
#define time3 "1550"
#define pages 2500
#define rows 3
#define cols 3
#define element 12
#define pagesize (rows*cols*element)
#define rowsize (cols*element)
#define size (pages*rows*cols*element)
char *PCRminus1;
char *PCRminus2;
char *PCRminus3;
char *PCR1;
int main (int argc, const char * argv[]) {
int i, d, t, n, found, amount, no, p;
char *filename, ** arr, *line;//, *symo;
FILE *datafile, *outputfile;
if (argc != (dates + 1))
{
printf(" Enter Three Dates, latest first\n");
return(0);
}
line = (char*)calloc(1024, sizeof(char));
filename = (char*)calloc(150, sizeof(char));
strcpy(filename,filedir);
strcat(filename, argv[1]);
strcat(filename, "_results.csv");
outputfile = fopen(filename, "w");
PCRminus3 = allocate_memory((char*)PCRminus3);
PCRminus2 = allocate_memory((char*)PCRminus2);
PCRminus1 = allocate_memory((char*)PCRminus1);
PCR1 = allocate_memory((char*)PCR1);
for (d = 1; d <= dates; d++)
for (t = 0; t < times; t++)
{
strcpy(filename,data);
strcat(filename,namestart);
strcat(filename,argv[d]);
strcat(filename,"_");
switch (t) {
case 2: strcat(filename,(char*) time1);
break;
case 1: strcat(filename,(char*) time2);
break;
case 0: strcat(filename,(char*) time3);
break;
}
strcat(filename,".csv");
printf("file %s\n", filename);
if ((datafile = fopen(filename, "r")) == NULL)
{
printf("Couldn't open file %s\n", filename);
exit(0);
}
fgets(line,1024,datafile);// discard first line And start comparing
while( (fgets(line,1024,datafile) != 0))
{
found = 0;
arr = parse(line,",");
for (i = 0; i < no + 1; i++)
{
if (strcmp (&PCR1[i*pagesize + 0*rowsize + 0*element], arr[3]) == 0)
{
found = 1;
p = i;
i = no + 4;
}
}
if (!found)
if ( (atoi(arr[2]) == 1) && ((atoi(arr[7]) == 3) || (atoi(arr[7]) == 4) || (atoi(arr[7]) == 5)) && (atoi(arr[8]) < 35) )
{
if((d == 1) && (t == 0))
{
sprintf(&PCR1[no*pagesize], "%s",arr[3]);
if (strcmp(arr[6],"P") == 0)
{
amount = atoi(arr[19]);
sprintf(&PCR1[(no++)*pagesize + d*rowsize + t*element], "%d", amount);
}
else
{
amount = atoi(arr[19]);
sprintf(&PCR1[(no++)*pagesize + (d+1)*rowsize * t*element], "%d", amount);
}
} //if found on day 1 end of day
}
} //while fgets
fclose(datafile);
} // t times
printf("%d \n", no);
n = 0;
for ( i = 0; i < no; i++)
{
if (i == 0)
fprintf(outputfile, "%s", &PCR1[i*pagesize]);
else
fprintf(outputfile, "\n%s ", &PCR1[i*pagesize]); }
free(PCR1);
free(filename);
for (i= 0; i <100; i++)
{
free(arr[i]);
}
free(arr);
fclose(outputfile);
printf("%d \n", n - 1);
return 0;
}