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;
} 2 5216 ba****************@yahoo.com (pradeep) wrote in message news:<ce**************************@posting.google. com>... 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 ba****************@yahoo.com (pradeep) wrote in message news:<ce**************************@posting.google. com>... 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mercuro |
last post by:
Hello
i'm looking for a simple way to checksum my data.
The data is 70 bytes long per record, so a 32
byte hex md5sum would increase the size of my
mysql db a lot.
I'm looking for something...
|
by: GujuBoy |
last post by:
is there a built-in function that does a "checksum" on a file...basicly
counts the bytes and computes a 16-bit checksum for each given FILE.
this is the like the "sum" command in unix
|
by: GujuBoy |
last post by:
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.
def...
|
by: Kevin |
last post by:
I'm on Sun 0S 5.8 and need to calculate the checksum of certain
sections in an ELF binary file. Specifically .text to .rodata. I'm
able to parse through to the program header table and then find...
|
by: Andrus |
last post by:
Device connected to serial port accepts data packets in the form
02 0x57 ll ll 00 00 00 00 dd..dd cc cc
02 (1 byte ) is message prefix
0x57 (1 byte) is message type (W=Write)
ll ll ( 2 bytes)...
|
by: Jesper |
last post by:
Gents,
I need the checksum of an xml file in order to verify that no changes has
been made to the file. How is this done.
Second, is it possible to 'embed' the MD5 checksum inside the very...
|
by: Bob |
last post by:
Hi there,
I am working on an application to be used in our local Forensics
department...
Among other things the app will connect to a digital camera and download the
images to the hard drive....
|
by: Zahid Faizal |
last post by:
Kindly suggest a good opensource package (in C or C++) that can compute
the checksum of a file. SHA2 would be preferable, but SHA1/SHA0/MD5
would be acceptable as well. We have cards with...
|
by: Kurt |
last post by:
Hi,
I'd like advices about an idea I add to resolve a problem. thanks to
you in advance for yours answers.
I have a database with tables that I load with flat file. The size of
each table is...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |