Y
Yu-Loong Liew
Hi all,
I have a question about file I/O. I am interested in processing a file with
unknown number of rows of data in the format of "xxx,xxx" to arrays. The
following code segment is what I have implemented. Any better way of doing
it?
int convert_data()
{
....
fstream = fopen (fileName, "r");
while (!feof(fstream))
{
fscanf (fstream, "%lf,%lf", &tempDouble, &tempDouble);
dataCount++;
}
rewind (fstream);
dataX = malloc (dataCount * sizeof(double));
dataY = malloc (dataCount * sizeof(double));
if (dataX == NULL || dataY == NULL)
{
printf("Unable to allocate memory!\n");
if (dataX != NULL) free (dataX);
if (dataY != NULL) free (dataY);
fclose(fstream);
exit(1);
}
for (idx = 0; idx < dataCount; idx++)
fscanf (fstream, "%lf, %lf", &dataX[idx], &dataY[idx]);
....
}
Thank you in advance!
Yu-Loong
I have a question about file I/O. I am interested in processing a file with
unknown number of rows of data in the format of "xxx,xxx" to arrays. The
following code segment is what I have implemented. Any better way of doing
it?
int convert_data()
{
....
fstream = fopen (fileName, "r");
while (!feof(fstream))
{
fscanf (fstream, "%lf,%lf", &tempDouble, &tempDouble);
dataCount++;
}
rewind (fstream);
dataX = malloc (dataCount * sizeof(double));
dataY = malloc (dataCount * sizeof(double));
if (dataX == NULL || dataY == NULL)
{
printf("Unable to allocate memory!\n");
if (dataX != NULL) free (dataX);
if (dataY != NULL) free (dataY);
fclose(fstream);
exit(1);
}
for (idx = 0; idx < dataCount; idx++)
fscanf (fstream, "%lf, %lf", &dataX[idx], &dataY[idx]);
....
}
Thank you in advance!
Yu-Loong