1

I try to calculate the checksum of the file in c.

I have a file of around 100MB random and I want to calculate the checksum.

I try this code from here: https://stackoverflow.com/a/3464166/14888108

    int CheckSumCalc(char * filename){
    FILE *fp = fopen(filename,"rb");
    unsigned char checksum = 0;
    while (!feof(fp) && !ferror(fp)) {
        checksum ^= fgetc(fp);
    }
    fclose(fp);
    return checksum;
}

but I got a Segmentation fault. in this line "while (!feof(fp) && !ferror(fp))"

Any help will be appreciated.

ATB
  • 119
  • 1
  • 9
  • That code is wrong. See [**Why is “while( !feof(file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) for starters, and [**7.21.7.1 The fgetc function**](http://port70.net/~nsz/c/c11/n1570.html#7.21.7.1), because `fgetc()` returns `int`, not `char`. – Andrew Henle Dec 03 '22 at 01:49

1 Answers1

1

The issue here is that you are not checking for the return value of fopen. fopen returns NULL if the file cannot be opened. This means that fp is an invalid pointer, causing the segmentation fault.

You should change the code to check for the return value of fopen and handle the error accordingly.

int CheckSumCalc(char * filename){
    FILE *fp = fopen(filename,"rb");
    if(fp == NULL)
    {
        //handle error here
        return -1;
    }
    unsigned char checksum = 0;
    while (!feof(fp) && !ferror(fp)) {
        checksum ^= fgetc(fp);
    }
    fclose(fp);
    return checksum;
}
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Thanks, its very Weird i use 100MB random file and its say the fp is NULL.. maybe you know what the problem is ? – ATB Dec 03 '22 at 01:02
  • @ATB The most likely problem is that you are not specifying the correct path to the file. Make sure you are either specifying an absolute path or a relative path (prefixed with './') that points to the correct file. – DotNetRussell Dec 03 '22 at 01:03
  • The file is the same project I use this program, I tried to put the full path same problem, i don't know whats worng – ATB Dec 03 '22 at 01:05
  • @ATB `fopen()` sets `errno` on failure. Print out the error, e.g. `fprintf(stderr, "fopen: '%s': %s\n", filename, strerror(errno))` – dimich Dec 03 '22 at 01:33
  • 1
    @dimich *`fopen()` sets `errno` on failure* [Not necessarily](https://port70.net/~nsz/c/c11/n1570.html#7.21.5.3p9): "The `fopen` function returns a pointer to the object controlling the stream. If the open operation fails, `fopen` returns a null pointer." It will on a [POSIX system, though](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/fopen.html#tag_16_155_04): " Otherwise, a null pointer shall be returned, and `errno` shall be set to indicate the error." – Andrew Henle Dec 03 '22 at 01:54
  • 1
    `while (!feof(fp) && !ferror(fp)) { checksum ^= fgetc(fp);;` is amiss for the usually [reasons](https://stackoverflow.com/q/5431941/2410359). Check the return value of `fgetc(fp` instead. – chux - Reinstate Monica Dec 03 '22 at 04:23