0

I'm having some problems with my code, the code does compile but when I test the code, I find that if the signature is at the end of the file(or after the middle of the file), the program doesn't work.

int normalScan(char * fileName, char * sign){
    int toReturn = 0;
    FILE * toScan = fopen(fileName,"rb");
    FILE *  signature = fopen(sign, "rb");
    char * buffer = NULL;
    char * secondBuffer = NULL;
    int size = 0;
    int secondSize = 0;
    int elements = 0;
    int i = 0;

    fseek(signature, 0, SEEK_END);
    size = ftell(signature);
    rewind(signature);

    fseek(toScan, 0, SEEK_END);
    secondSize = ftell(toScan);
    rewind(toScan);

    buffer = malloc(sizeof(char)*size);
    elements = fread(buffer, 1, size, signature);

    secondBuffer = malloc(sizeof(char)*size);
    elements = fread(secondBuffer, 1, size, toScan);

    if (strcmp(secondBuffer, buffer) == 0){
        toReturn = 1;
    }
    else{
        while (fgetc(toScan) != EOF && fgetc(signature) != EOF){
            rewind(signature);
            fseek(toScan, -1, SEEK_CUR);
            if (fgetc(toScan) == fgetc(signature)){
                rewind(signature);
                fseek(toScan, -1, SEEK_CUR);
                elements = fread(secondBuffer, 1, size, toScan);

                if (strcmp(secondBuffer, buffer) == 0){
                    toReturn = 1;
                }
            }

            rewind(signature);
            strncpy(secondBuffer, "", sizeof(secondBuffer));
        }
    }

    free(secondBuffer);
    fclose(toScan);
    fclose(signature);
    free(buffer);

    return toReturn;
}
Saga
  • 67
  • 1
  • 8
  • 2
    Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh May 18 '17 at 06:38
  • 2
    [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv May 18 '17 at 06:46
  • 1
    You may want to say a couple of words about what your code intends to do. It's not very clear from the code itself. Is it searching for a known signature in a file? – n. m. could be an AI May 18 '17 at 06:49
  • There is a problem with that algorithm: You seem to be reading your file to scan in chunks the size of the signature - In case your signature occurs *shifted* in the scan file, it will overlap two such chunks - And you won't find it. Re-think that. Your scan file chunks should be much bigger than the signature and *overlap by the size of the signature* – tofro May 18 '17 at 06:59
  • @tofro Thanks, but I still don't get it. First I check if the first char in the signature file and the first(then the second, third and so on) char in the scan file are equal, in case they are, I create a buffer in the size of the signature and buffer of the signature file, and check if they are equal. For example: Signature buffer: Y-XXXZZZLLL Scan buffer: XXXZZZLLL X != Y X != - X = X And then I check if: XXXZZZLLL = XXXZZZLLL. Can you please explain to me again? – Saga May 18 '17 at 07:11
  • Try this: signature is 123, file is 1123. – n. m. could be an AI May 18 '17 at 07:20
  • 1
    Why do you keep reading back and forth in the signature file? Looks a bit confused to me. You only need to read that once - Then it's in your `buffer` variable, ready to be compared. – tofro May 18 '17 at 07:22

1 Answers1

1

You have 4 problems :

1) You not checking for EOF signature in this scope:

while (!feof(toScan)){
      if (fgetc(toScan) == fgetc(signature)){
           fseek(signature, 0, SEEK_SET);

You may check fgetc(toScan) and fgetc(signature) for EOF or use !feof(signature). If signature is not present and file have 0 size - you in trouble. You shouldn't rely on case that file will be always have some data

2) Your code possibly contains a algorithm mistake. Check step-by-step your code exectution with debuger or just following the code lines.

3) Check that fopen returns non-zero value; Check that file have been successfully opened.

Also fseek(signature, 0, SEEK_SET); and rewind(signature); can be striped down to rewind(signature) in this case:

     while (fgetc(toScan) != EOF && fgetc(signature) != EOF){
        // deleted rewind
        fseek(toScan, -1, SEEK_CUR);
        if (fgetc(toScan) == fgetc(signature)){
            rewind(signature);
            fseek(toScan, -1, SEEK_CUR);
            elements = fread(secondBuffer, 1, size, toScan);

            if (strcmp(secondBuffer, buffer) == 0){
                toReturn = 1;
            }
        }

        rewind(signature);
        strncpy(secondBuffer, "", sizeof(secondBuffer));
    }

with this:

     while (fgetc(toScan) != EOF && fgetc(signature) != EOF){
        rewind(signature);
        fseek(toScan, -1, SEEK_CUR);
        if (fgetc(toScan) == fgetc(signature)){
            // deleted rewind
            fseek(toScan, -1, SEEK_CUR);
            elements = fread(secondBuffer, 1, size, toScan);

            if (strcmp(secondBuffer, buffer) == 0){
                toReturn = 1;
            }
        }

        rewind(signature);
        strncpy(secondBuffer, "", sizeof(secondBuffer));
    }

because it will always set signature cursor to 0.

4) You looking like you don't fully understands how to code on C. Please read C Reference and read Herbert Schildt books about C. The soulution for you is:


Here some tips for you:

1) Check your algorithm (is it works or not)

2) Rewrite your code (try to accomplish your goal with simple code, use standart std*.h functions)

3) Debug your program with the debuger (use IDE, compiler flags and etc.).

Mikhail Romanko
  • 396
  • 1
  • 9
  • Thanks, I changed my algorithm a bit and It worked. By the way I want to ask you how can I set the while loop to stop when it reaches the middle of the file instead the end? – Saga May 18 '17 at 12:49