Saga
Saga

Reputation: 77

File signature C

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;
}

Upvotes: 0

Views: 1579

Answers (1)

Mikhail Romanko
Mikhail Romanko

Reputation: 396

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.).

Upvotes: 1

Related Questions