Okay...so here's where it's at:
#include <stdio.h>
#include <stdlib.h>
struct sRecord
{
int date, volume;
char time;
double price[4];
};
int main(void)
{
static const char filename[] = "file.csv";
FILE *file = fopen(filename, "r");
printf("file opened\n");
system("pause");
if ( file )
{
size_t i = 0, size = 30;
struct sRecord *array = malloc(size * sizeof *array);
printf("assigned memory to array address\n");
system("pause");
if ( array )
{
char line[128];
printf("entering While loop\n");
system("pause");
while ( i < size && fgets(line, sizeof line, file) )
{
if ( sscanf(line, "%d,%c,%lf,%lf,%lf,%lf,%ld",
&array[i].date, &array[i].time,
&array[i].price[0], &array[i].price[1],
&array[i].price[2], &array[i].price[3],
&array[i].volume) == 7 )
{
++i;
}
}
printf("leaving While loop\n");
system("pause");
fclose(file);
printf("Closed File\n");
system("pause");
/* Test first three rows of Array */
printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[0].date, array[0].time,
array[0].price[0], array[0].price[1], array[0].price[2],
array[0].price[3], array[0].volume);
printf("\n");
printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[1].date, array[1].time,
array[1].price[0], array[1].price[1], array[1].price[2],
array[1].price[3], array[1].volume);
printf("\n");
printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[2].date, array[2].time,
array[2].price[0], array[2].price[1], array[2].price[2],
array[2].price[3], array[2].volume);
printf("Test Row\n");
system("pause");
/*
* [Modify contents]
*/
for ( i = 0; i < size; ++i )
{
printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[i].date, array[i].time,
array[i].price[0], array[i].price[1], array[i].price[2],
array[i].price[3], array[i].volume);
}
printf("File printed\n");
system("pause");
free(array);
}
}
else
{
perror(filename);
}
system("pause");
return 0;
}
Here's the test data:
20041201,0:00,102.74,102.77,102.74,102.76,5
20041201,0:05,102.78,102.78,102.75,102.75,3
20041201,0:10,102.75,102.75,102.7,102.7,7
20041201,0:15,102.7,102.72,102.7,102.72,8
20041201,0:20,102.73,102.74,102.73,102.74,2
20041201,0:25,102.73,102.73,102.7,102.7,4
20041201,0:30,102.71,102.71,102.68,102.7,10
20041201,0:35,102.71,102.73,102.68,102.69,15
20041201,0:40,102.7,102.71,102.69,102.71,12
20041201,0:45,102.71,102.73,102.71,102.73,2
20041201,0:50,102.74,102.78,102.74,102.77,12
20041201,0:55,102.77,102.8,102.77,102.79,17
Here's the output:
file opened
Press any key to continue . . .
assigned memory to array address
Press any key to continue . . .
entering While loop
Press any key to continue . . .
leaving While loop
Press any key to continue . . .
Closed File
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
Test Row
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
File printed
Press any key to continue . . .
My Questions:
1) How is malloc supposed to provide the correct amount of memory if it doesn't even know what's stored in array[] yet? Doesn't array[] get filled in the loop below the allocation statement? So it won't know the size until then?
2) The program is only printing the correct first date in the list and then prints 0's for everything else...anyone see a reason?
Thanks for the help,
Nolan