Thursday, July 30, 2009

How do I read from two or more external FILE's in 'C', (NOT C++)?

There are two external FILES, this 100 integers each.





How do I get the C code to read the two files, stop at the first integer in each file, then compare which is the higher of the two, then print to screen?








Then to loop again, to compare the next interger, of each file, until EOF.

How do I read from two or more external FILE's in 'C', (NOT C++)?
visit my blog





http://codesbyshariq.blogspot.com


for example and source codes
Reply:the functions you'll need are fopen, fread, fclose and atoi (string to int).





something like:





FILE *f1 = fopen("myfile1.txt","r");


FILE *f2 = fopen("myfile2.txt","r");


void *buff1, buff2;


size_t size = sizeof(int); //for fread





while (EOF != fread(buff1,size,1,f1) %26amp;%26amp; EOF != fread(buff2,size,1,f2)) {


int i1 = atoi(buff1);


int i2 = atoi(buff2);


if(i1 %26gt; i2) { printf("file 1's line is larger\n"); }


else if(i2 %26gt; i1) { printf("file 2's line is larger\n"); }


else { printf("equal.\n"); }


}





fclose(f1);


fclose(f2);








hard-core C programmers are probably laughing at this code, and it certainly won't compile, but it's in the right direction.


No comments:

Post a Comment