Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string::append appening twice

5 views
Skip to first unread message

Rahul

unread,
Jun 7, 2009, 7:09:50 AM6/7/09
to
Hi Everyone,

I have the following code,

char temp[1024];
sprintf(temp,"_%d_%d",type,value);
FileName.append(temp); //<file_name>_1_2

however, i noticed that _1_2 is appended twice and i get an output
of <file_name>_1_2_1_2 instead of just <file_name>_1_2.
Is there any side effect to the append member function call?

Thanks in advance ! ! !

Neelesh

unread,
Jun 7, 2009, 9:24:14 AM6/7/09
to

Please post the exact code that shows this issue. The following code
does exactly what you have asked and it also prints exactly what you
want.

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main()
{
int type = 1;
int value = 2;


char temp[1024];
sprintf(temp,"_%d_%d",type,value);

string FileName = "hello.txt";
FileName.append(temp);
cout << FileName << endl;
}

blargg

unread,
Jun 7, 2009, 9:36:04 AM6/7/09
to
Rahul wrote:
> char temp[1024];
> sprintf(temp,"_%d_%d",type,value);
> FileName.append(temp); //<file_name>_1_2
>
> however, i noticed that _1_2 is appended twice and i get an output
> of <file_name>_1_2_1_2 instead of just <file_name>_1_2.
> Is there any side effect to the append member function call?

The parts you've omitted could be part of your problem. For example, you
might be calling said function twice, or the way you're printing the
result might be flawed. Try posting a small, complete program that
demonstrates the problem.

0 new messages