Hi,
I have the below sample code to hash the input number read from file. File will have 16 to 19 digit number and executable hash the number using some logic and returns the hashed value. Each digit in the 16 digit number is converted to a 4 byte value. That if the input is 16digit integer, the output is 64byte string. I have split the code in to two. One main function and other file having this hashing function as shown below:
MAIN:
ganesh@ubuntu:~/my_programs/c$ cat main.c
# include "fnhash.h"
# include <stdio.h>
int main(int argc, char *argv[])
{
if(argc!=2)
{
printf("Usage wrong");
exit(1);
}
FILE *fp;
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("File opening error\n");
exit(1);
}
char *instr;
char hashnum[100];
while(!feof(fp))
{
while(fscanf(fp,"%s",instr)>0)
{
printf("in Number Length : %lu\n",strlen(instr));
instr[strlen(instr)+1]='\0';
printf("in Number = %s\n",instr);
fnhash(instr,hashnum);
printf("in number : %s\tHashed value : %s\n",instr,hashnum);
}
}
return 0;
}
FUNCTION:
ganesh@ubuntu:~/my_programs/c$ cat fnhash.c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "fnhash.h"
void fnhash(const char nbr[],char hsh[])
{
char *tmpnbr, *tmphsh;
tmpnbr=nbr;
tmphsh=hsh;
char a;
char b;
while(*tmpnbr!='\0')
{
a=('A'-*tmpnbr)+'A';
b=('z'+*tmpnbr)-'A';
sprintf(tmphsh,"%d%c%c",*tmpnbr,a,b);
tmpnbr++;
tmphsh=tmphsh+4;
}
printf("strlen of hashed value = %lu\n",strlen(hsh));
tmphsh[strlen(tmphsh)+1]='\0';
return;
}
The file having the numbers is as below:
ganesh@ubuntu:~/my_programs/c$ cat numbers.lst
1234567890123456
12345678901234567
1234567890123456789
123456
Execution Step as follows:
ganesh@ubuntu:~/my_programs/c$ make -f hashmake
gcc -Wall -g -c main.c
main.c: In function �main':
main.c:9: warning: implicit declaration of function �exit'
main.c:9: warning: incompatible implicit declaration of built-in function �exit'
main.c:16: warning: incompatible implicit declaration of built-in function �exit'
main.c:26: warning: implicit declaration of function �strlen'
main.c:26: warning: incompatible implicit declaration of built-in function �strlen'
main.c:26: warning: �instr' may be used uninitialized in this function
gcc -Wall -g main.o fnhash.o -o main
ganesh@ubuntu:~/my_programs/c$ ./main numbers.lst
in Number Length : 16
in Number = 1234567890123456
Segmentation fault (core dumped)
On Analysis with GDB,
ganesh@ubuntu:~/my_programs/c$ gdb main core
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ganesh/my_programs/c/main...done.
[New Thread 3605]
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `0645Uf49Qj49Qj56Jq'.
Program terminated with signal 11, Segmentation fault.
#0 0x00007f578f8fbb87 in vsprintf () from /lib/libc.so.6
What is the possible reason for this error?? Where Am I going wrong??
Thanks,
Ramkrix