Sponsored Content
Full Discussion: Problem with Sprintf
Top Forums Programming Problem with Sprintf Post 302464649 by ramkrix on Wednesday 20th of October 2010 04:18:58 PM
Old 10-20-2010
Power Problem with Sprintf

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:
Code:

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:
Code:
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:
Code:
ganesh@ubuntu:~/my_programs/c$ cat numbers.lst
1234567890123456
12345678901234567
1234567890123456789
123456

Execution Step as follows:

Code:
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,

Code:
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

Last edited by Scott; 10-20-2010 at 05:26 PM..
 

10 More Discussions You Might Find Interesting

1. Solaris

problem with sprintf snprintf in Solaris

******************************** Following is not the real issue. The issue is with popen. Plz continue forward with the thread to get a better picture. ******************************** Hi, I am working on a customised ftp application. In it we have used sprintf to store a UNIX command... (7 Replies)
Discussion started by: diganta
7 Replies

2. Programming

sprintf function

Hi, Can someone help me to figure out whether this code is to write file to /tmp/TIMECLOCK directory or just to asign a variable with "/tmp/TIMECLOCK/name.log_copy.pid" as the string? I am looking into an old C program and could not figure out where in the code that creates... (1 Reply)
Discussion started by: whatisthis
1 Replies

3. Shell Programming and Scripting

ksh scripting sprintf

is there any sprintf function in korn shell scripting, or anything similar to sprintf? (2 Replies)
Discussion started by: gfhgfnhhn
2 Replies

4. Programming

equivalent of sprintf in C++

Hi My requirement is to convert the following to C++ char buffer; sprintf(buffer,"%s %-50s %6s %-6d %s\n",a.substr(0,5),a.substr(10,20)) Since the buffer is of varying length, i cannot hardcode the value as 90. i would like to convert the buffer to string object so that it can receive any... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

5. Shell Programming and Scripting

sprintf result on perl

Hello, In perl lang, I have create a string (@str) by sprintf but unfortunately when program printed it out, only I could saw a number like 1. Certainly printf doesn't problem. How I can convert a string that are result of sprintf to a common string format??! Thanks in advance. PLEASE HELP ME. (2 Replies)
Discussion started by: Zaxon
2 Replies

6. Shell Programming and Scripting

printf vs sprintf - perl

I would like to assign the output of printf to a variable in perl , it give me back a "1" instead of the time. How can I stuff the variable with what printf returns? Here is my code: #!/usr/bin/perl $time = localtime(time);... (3 Replies)
Discussion started by: Dabheeruz
3 Replies

7. Shell Programming and Scripting

Detect sprintf and fprintf bad use

Hello again, I don't know about regexp so I throw this question here: How can I detect files where, for example: sprintf (var1, "hello %s %s", sub1); The problem here is that we have 2 %s and only a variable. Or... the inverse: sprintf (var1, "hello %s %s", sub1, sub2, sub3,...subn); ... (2 Replies)
Discussion started by: albertogarcia
2 Replies

8. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

9. Programming

How to right pad with zeros using sprintf?

I need to right-pad with zeros a string by using (s)printf. I looked up the manual and tried with printf("%-19s", buffer); which right-pad the string with spaces. So I tried printf("%019s", buffer); which left-pad the string with zeros. So I tried both printf("%-019s", buffer);... (9 Replies)
Discussion started by: emitrax
9 Replies

10. UNIX for Dummies Questions & Answers

Join with awk using sprintf

Hi, Trying to join 2 files with awk (file1 has variable number of fields; file 2 has constant number of fields) file1: hook1|AA|BB|CC|DD hook2|EE|FF file2: hook1|11|22 hook2|33|44 hook3|55|66 output: hook1|11|22|AA|BB|CC|DD hook2|33|44|EE|FF hook3|55|66 What I tried so far:... (3 Replies)
Discussion started by: beca123456
3 Replies
All times are GMT -4. The time now is 09:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy