473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help concatentating 2 strings and an int.

Hi - please help. I have two strings and a number which I need to concatenate together and I'm confused to what is the best approach. Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. char *s1 = "iamafilename_";
  3. char *s2 = ".tif";
  4.  
  5. int i;
  6. for( i = 0; i < 10; i++ )
  7. {
  8.  
  9.  char itoaBuff[100];
  10.  int num_len = sprintf(itoaBuff, "%s", i);
  11.  
  12.  char *f = (char *)malloc( (strlen(s1) + strlen(s2) + num_len) * sizeof(char) + 1 );
  13.  
  14.  strcat( f, s1 );
  15.  strcat( f, itoaBuff );
  16.  strcat( f, s2 );
  17.  
  18.  printf("the three strings concatenated is: %s\n\n", f);
  19.  
  20.  free( f );
  21.  
  22. }
  23.  
  24.  


i get a seg fault by the way. can anyone see what i am trying to do and advise me please?
Jul 14 '11 #1

✓ answered by weaknessforcats

What I notice is:

Expand|Select|Wrap|Line Numbers
  1. strcat( f, s1 ); 
  2.  strcat( f, itoaBuff ); 
  3.  strcat( f, s2 ); 
strcat operates by appending after a \0 which your buffer f may not have. I suggest an initial strcpy then the strcats:

Expand|Select|Wrap|Line Numbers
  1. strcpy( f, s1 ); 
  2.  strcat( f, itoaBuff ); 
  3.  strcat( f, s2 ); 
or at least set f[0] to \0 before the strcats.

Without that initial \0 your strcat will append an indeterminate number of bytes. That could cause you to overrun f and corrupt memory.

4 1979
I wish I could delete this post because I realized my sprintf needed %d inside and it worked. I'm smarter than I thought. pat pat
Jul 14 '11 #2
or would it have been better to use scanf ??
Jul 14 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
What I notice is:

Expand|Select|Wrap|Line Numbers
  1. strcat( f, s1 ); 
  2.  strcat( f, itoaBuff ); 
  3.  strcat( f, s2 ); 
strcat operates by appending after a \0 which your buffer f may not have. I suggest an initial strcpy then the strcats:

Expand|Select|Wrap|Line Numbers
  1. strcpy( f, s1 ); 
  2.  strcat( f, itoaBuff ); 
  3.  strcat( f, s2 ); 
or at least set f[0] to \0 before the strcats.

Without that initial \0 your strcat will append an indeterminate number of bytes. That could cause you to overrun f and corrupt memory.
Jul 14 '11 #4
thank you weaknessforcats because i was going crazy with a seg fault, and thought it might have something to do with all strcat's, and you validated it with good info. now i know what is the correct approach.
Jul 15 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
41
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it...
4
by: Marco Krechting | last post by:
Hi All, Sorry but I have to create new message since it cannot find the old message to send a reply. Coming back to this hyperlink thing I will try to explain the real problem cause I think we...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
1
by: Dave | last post by:
Hello All, I'm trying to clarify how Python avoids byte by byte string comparisons most of the time. As I understand, dictionaries keep strings, their keys (hash values), and caches of their...
7
by: temp34k45k | last post by:
I need to evaluate two strings for their order. The strings contain Letters (A thru Z upper case only) and Numbers (0-9) and the decimal point (.). I need an order like the list that follows: ...
7
by: p.lavarre | last post by:
Subject: announce: FAQs suggested ... That suggested FAQ is misleadingly incorrect as stated - we need help rewording it. /F correctly commented: "eval" is never a good choice if you cannot...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
4
by: Alexey Moskvin | last post by:
Hi! I have a set of strings (all letters are capitalized) at utf-8, russian language. I need to lower it, but my_string.lower(). Doesn't work. See sample script: # -*- coding: utf-8 -*- s1 =...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.