reading a file

C

c language

Hello everybody,

I have to do some calculations on two different files with C program.
First file has 2 columns and second file has 100 columns. The program
should get the columns of the first file and 2 columns of the second
file and do some calculation on different rows of those files. It
should repeat this action for all the columns of the second file which
it needs to be repeated 50 times for all 50 pairs columns of the second
file.
I do not have any problem for the calculation part but I am not sure
how I can write a program to go and read the columns of a file one by
one.
Is there any body to give me some clues?

Thanks,
MJK
 
M

Michael Mair

c said:
I have to do some calculations on two different files with C program.
First file has 2 columns and second file has 100 columns. The program
should get the columns of the first file and 2 columns of the second
file and do some calculation on different rows of those files. It
should repeat this action for all the columns of the second file which
it needs to be repeated 50 times for all 50 pairs columns of the second
file.
I do not have any problem for the calculation part but I am not sure
how I can write a program to go and read the columns of a file one by
one.
Is there any body to give me some clues?

Yep. Read the files line by line[*] and read numbers using
strtod(). Depending on whether you aim for less memory use
or less overall time, (1) discard unnecessary numbers or (2)store
all columns. Make sure that even if you have read 100 numbers
there is no 101st left on the line.

Consider starting like this (or similar) and write the missing
functions.

/*
return 0 on success
pair: there is a 0th pair
max: number of expected column entries; pass 0 to say you
don't care
*/
int GetColumnPair(FILE *pFile, size_t pair, size_t max,
double (**ppCols)[2], size_t *plen)
{
size_t clen = 0, buflen = 0;
char *buf = 0;

if (NULL == ppCols) {
return -1;
}

clen = 0;

while (EOF != ReadLine(fp, &buf, &buflen))
{
if (clen >= *plen) {
/* realloc ppCols, adjust plen */
}
if (GetTwoNumbers(buf, 2*pair, max, *pCols)) {
DiscardLineBuffer(&buf);
return clen<INTMAX?clen:-2;
}
clen++;
DiscardLine(&buf, &buflen);
}

DiscardLineBuffer(&buf);

return 0;
}

[*]There are many, many, many threads in the comp.lang.c archives
dealing with that. Consider using fgets() or the public domain fggets().
DiscardLine() may or may not be necessary.

Cheers
Michael
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top