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