File I/O

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
 
A

Artie Gold

Yu-Loong Liew said:
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");

You need to see if the fopen() call succeeded.
while (!feof(fstream))

NO! Please see:

http://www.eskimo.com/~scs/C-faq/q12.2.html

(you *did* read the FAQ *first* didn't you?)
{
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]);

...
}
Why read the file twice? If it's large, you'd be better off starting
your buffer(s) at some reasonable size and using realloc() to double the
size as needed (which would be O(log n) realloc()s).

Of course, if the file is small it doesn't matter much. ;-)

HTH,
--ag
 
A

Allan Bruce

Yu-Loong Liew said:
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);

be sure to check the return value of fscanf to see if it has assigned the
correct number of arguements.
Allan
 
Y

Yu-Loong Liew

Sorry, I didn't read the FAQ.

Thanks for your help.

Yu-Loong

Artie Gold said:
Yu-Loong Liew said:
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");

You need to see if the fopen() call succeeded.
while (!feof(fstream))

NO! Please see:

http://www.eskimo.com/~scs/C-faq/q12.2.html

(you *did* read the FAQ *first* didn't you?)
{
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]);

...
}
Why read the file twice? If it's large, you'd be better off starting
your buffer(s) at some reasonable size and using realloc() to double the
size as needed (which would be O(log n) realloc()s).

Of course, if the file is small it doesn't matter much. ;-)

HTH,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top