Omer
Omer

Reputation: 96

Compare 2 FILEs in C by Size

I have this code,but its throw an error in line 5(fseek).

int cmpFILEd(void *aa,void *bb){
    int size1,size2;
    FILE *a=(FILE*)aa;
    FILE *b=(FILE*)bb;
    fseek(a,0,SEEK_END);
    fseek(b,0,SEEK_END);
    size1=ftell(a);
    size2=ftell(b);
    if(size1==size2)
        return 0;
    else if(size1>size2)
        return 1;
    else 
        return -1;
}

What is the problem in this code?

_

EDIT:The error:

First-chance exception at 0x77052865 (ntdll.dll) in cFILE.exe: 0xC0000008: An invalid handle was specified.

If there is a handler for this exception, the program may be safely continued.

Upvotes: 1

Views: 144

Answers (3)

chux
chux

Reputation: 154272

Given that:
1) OP "... cant changed the void its need to be generic."
2) int cmpFILEd(void *aa,void *bb) is a compare function

This looks like an ill formed compare function for qsort() or such.

In that case, the pointers passed are addresses of FILE *, as suggested by @BLUEPIXY,

int cmpFILEd(void *aa,void *bb){
  long size1, size2;  // Note use long here
  FILE *a = *((FILE**) aa);
  FILE *b = *((FILE**) bb);
  ...

Upvotes: 1

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6013

Perhaps if you were to simplify the code?

int cmpFILEd(FILE *a, FILE *b)
   {
   fseek(a, 0, SEEK_END);
   fseek(b, 0, SEEK_END);

   return ftell(a) > ftell(b) ? 1 : 0;
   }

If the function definition cannot change, perhaps:

int cmpFILEd(void *a, void *b)
   {
   fseek((FILE *)a, 0, SEEK_END);
   fseek((FILE *)b, 0, SEEK_END);

   return ftell((FILE *)a) > ftell((FILE *)b) ? 1 : 0;
   }

A less academic version might help diagnose the problem:

int cmpFILEd(void *a, void *b)
   {
   long aSize=0;
   long bSize=0;

   errno=0;
   if((-1) == fseek((FILE *)a, 0, SEEK_END))
      {
      fprintf(stderr, "fseek(a...) failed.  errno:%d\n");
      goto CLEANUP;
      }

   errno=0;
   if((-1) == fseek((FILE *)b, 0, SEEK_END))
      {
      fprintf(stderr, "fseek(b...) failed.  errno:%d\n");
      goto CLEANUP;
      }

   errno=0;
   if((-1) == (aSize=ftell((FILE *)a)))
      {
      fprintf(stderr, "ftell(a) failed.  errno:%d\n");
      goto CLEANUP;
      }

   errno=0;
   if((-1) == (bSize=ftell((FILE *)b)))
      {
      fprintf(stderr, "ftell(b) failed.  errno:%d\n");
      goto CLEANUP;
      }

CLEANUP:

   return(aSize > bSize ? 1 : 0);
   }

Upvotes: 1

Mickey
Mickey

Reputation: 490

If you really insist the void* and not FILE*, it will work if you send 2 FILE*.

I run the following code:

#include <stdio.h>

int cmpFILEd(void *aa,void *bb){
    int size1,size2;
    FILE *a=(FILE*)aa;
    FILE *b=(FILE*)bb;
    fseek(a,0,SEEK_END);
    fseek(b,0,SEEK_END);
    size1=ftell(a);
    size2=ftell(b);
    if(size1==size2)
        return 0;
    else if(size1>size2)
        return 1;
    else 
        return -1;
}

int main(){
  FILE* f1 = fopen("file1","r");
  FILE* f2 = fopen("file2","r");
  int x = cmpFILEd(f1,f2);
  printf("%d\n", x);
  return 0;
}

I got -1 on input of 2 files with the contents "first" and "second" respectively.

Upvotes: 1

Related Questions