checksum in the end of the file

Discussion in 'C Programming' started by pradeep, Jul 9, 2003.

  1. pradeep

    pradeep Guest

    I have 2 data files,
    DATA1 and DATA2 , both same.
    My task is to:
    Open DATA1, compute the checksum and put it in the end of the
    file(don't bother about boundary conditions).close DATA1

    Open DATA2,compute the checksum and put it in the end(don't bother
    about boundary conditions).close DATA2.

    Now again open DATA1, compute the checksum of the file(leaving the
    checksum value stored in the end) and then compare it with checksum
    stored in the end.Return TRUE or FALSE as the case may be.

    Same for DATA2 in case check for DATA1 fails.

    Here is my program but I am not getting proper results..
    Any help would be appreciated.
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <string.h>
    int main()
    {
    FILE *fp;

    unsigned int p = 0;
    unsigned int i = 0;
    char ch;
    unsigned int size;
    fp=fopen("/users/pradeepb/c_progs/DATA1","r+");
    if(fp == NULL) {
    printf("file not exist\n");
    exit(1);
    }
    while((ch=getc(fp)) != EOF) {
    p+=ch;
    }
    fseek(fp,0L,SEEK_END);
    fwrite(&p,sizeof(unsigned int),1,fp);
    fclose(fp);
    p=0;
    fp=fopen("/users/pradeepb/c_progs/DATA2","r+");
    if(fp == NULL) {
    printf("file not exist\n");
    exit(1);
    }
    while((ch=getc(fp)) != EOF) {
    p+=ch;
    }
    fseek(fp,0L,SEEK_END);
    fwrite(&p,sizeof(unsigned int),1,fp);
    fclose(fp);

    fp=fopen("/users/pradeepb/c_progs/DATA1","r");

    p=0;
    if(fp == NULL)
    exit(1);


    while((ch=getc(fp)) != EOF) {
    p+=ch;
    }

    fseek(fp,0L,SEEK_END);
    size = ftell(fp);
    fseek(fp,size-sizeof(unsigned),SEEK_SET);

    i=0;
    while((ch=getc(fp)) != EOF) {
    i+=ch;
    }

    printf(" %d %d",p,i);
    if(p-i == i)
    printf("correct checksum of original file yahoo!!\n");
    fclose(fp);
    fp=fopen("/users/pradeepb/c_progs/DATA2","r");
    p=0;
    i=0;
    if(fp == NULL)
    exit(1);
    while((ch=getc(fp)) != EOF) {
    p+=ch;
    }
    fseek(fp,0L,SEEK_END);
    size = ftell(fp);
    fseek(fp,size-sizeof(unsigned),SEEK_SET);
    while((ch=getc(fp)) != EOF) {
    i+=ch;
    }
    printf(" %d %d",p,i);

    if(p-i == i)
    printf("correct checksum of checksum file yahoo!!\n");




    return 0;
    }
     
    pradeep, Jul 9, 2003
    #1
    1. Advertising

  2. (pradeep) wrote in message news:<>...

    > Now again open DATA1, compute the checksum of the file(leaving the
    > checksum value stored in the end) and then compare it with checksum
    > stored in the end.Return TRUE or FALSE as the case may be.


    I think the problem is it. Try to store the original checksum in the
    memory, remove it from the file, and so do the new checksum.

    The first checksum was made with the original file, the second not.

    -- Luiz Capitulino
     
    Luiz Capitulino, Jul 9, 2003
    #2
    1. Advertising

  3. pradeep

    Eric Bernard Guest

    (pradeep) wrote in message news:<>...
    > I have 2 data files,
    > DATA1 and DATA2 , both same.
    > My task is to:
    > Open DATA1, compute the checksum and put it in the end of the
    > file(don't bother about boundary conditions).close DATA1
    >
    > Open DATA2,compute the checksum and put it in the end(don't bother
    > about boundary conditions).close DATA2.
    >
    > Now again open DATA1, compute the checksum of the file(leaving the
    > checksum value stored in the end) and then compare it with checksum
    > stored in the end.Return TRUE or FALSE as the case may be.
    >
    > Same for DATA2 in case check for DATA1 fails.
    >
    > Here is my program but I am not getting proper results..
    > Any help would be appreciated.
    > #include <stdio.h>
    > #include <sys/types.h>
    > #include <unistd.h>
    > #include <string.h>
    > int main()
    > {
    > FILE *fp;
    >
    > unsigned int p = 0;
    > unsigned int i = 0;
    > char ch;
    > unsigned int size;
    > fp=fopen("/users/pradeepb/c_progs/DATA1","r+");
    > if(fp == NULL) {
    > printf("file not exist\n");
    > exit(1);
    > }
    > while((ch=getc(fp)) != EOF) {
    > p+=ch;
    > }
    > fseek(fp,0L,SEEK_END);
    > fwrite(&p,sizeof(unsigned int),1,fp);
    > fclose(fp);
    > p=0;
    > fp=fopen("/users/pradeepb/c_progs/DATA2","r+");
    > if(fp == NULL) {
    > printf("file not exist\n");
    > exit(1);
    > }
    > while((ch=getc(fp)) != EOF) {
    > p+=ch;
    > }
    > fseek(fp,0L,SEEK_END);
    > fwrite(&p,sizeof(unsigned int),1,fp);
    > fclose(fp);
    >
    > fp=fopen("/users/pradeepb/c_progs/DATA1","r");
    >
    > p=0;
    > if(fp == NULL)
    > exit(1);
    >
    >
    > while((ch=getc(fp)) != EOF) {
    > p+=ch;
    > }
    >
    > fseek(fp,0L,SEEK_END);
    > size = ftell(fp);
    > fseek(fp,size-sizeof(unsigned),SEEK_SET);
    >
    > i=0;
    > while((ch=getc(fp)) != EOF) {
    > i+=ch;
    > }
    >
    > printf(" %d %d",p,i);
    > if(p-i == i)
    > printf("correct checksum of original file yahoo!!\n");
    > fclose(fp);
    > fp=fopen("/users/pradeepb/c_progs/DATA2","r");
    > p=0;
    > i=0;
    > if(fp == NULL)
    > exit(1);
    > while((ch=getc(fp)) != EOF) {
    > p+=ch;
    > }
    > fseek(fp,0L,SEEK_END);
    > size = ftell(fp);
    > fseek(fp,size-sizeof(unsigned),SEEK_SET);
    > while((ch=getc(fp)) != EOF) {
    > i+=ch;
    > }
    > printf(" %d %d",p,i);
    >
    > if(p-i == i)
    > printf("correct checksum of checksum file yahoo!!\n");
    >
    >
    >
    >
    > return 0;
    > }



    I see many problems in that piece of code.

    Firstly, you are using a char type with getc, which returns int (yes,
    it matters). Also, "char ch" is signed, which means that if the file
    has a byte with a value of 128 to 255, ch will be negative thus p +=
    ch; decrements p.

    Secondly, the checksum you store in the end is based on 2 or four
    bytes in integer representation. When you read it back, you still add
    up its bytes but that's not the same thing as reading it as an actual
    integer. For example, if the checksum was 256, or 0x00000100, your
    computation would give 1 instead of 256 when you read it back.

    That's all I bothered to read for now, but I'm sure it will help.

    Kind regards

    Eric Bernard.
     
    Eric Bernard, Jul 9, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    3
    Views:
    16,429
    Roedy Green
    May 7, 2006
  2. Morris Dovey

    Re: How to store a checksum value at end of file?

    Morris Dovey, Jun 24, 2003, in forum: C Programming
    Replies:
    2
    Views:
    954
    Mark McIntyre
    Jun 25, 2003
  3. Dave Thompson

    Re: How to store a checksum value at end of file?

    Dave Thompson, Jul 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,273
    Dave Thompson
    Jul 4, 2003
  4. Jimmy
    Replies:
    3
    Views:
    358
    J. J. Farrell
    Sep 9, 2011
  5. Jimmy
    Replies:
    13
    Views:
    674
    Peter Nilsson
    Sep 21, 2011
Loading...

Share This Page