How to attach a string to an integer value

Hi
I have "/tmp." as my string value and also an integer value which has been generated and I want to attach them together so I can use it as a name file in open/fopen function

So how can I do it ?
thanks
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

    string
        OutString = "",
        TempString = "/tmp";

    stringstream ss;

    /*    Assume you generate your integer value with getpid():    */

    ss << TempString << getpid();

    ss >> OutString;

    cout << "OutString is now: [" << OutString << "]" << endl;


thanks man
but my main problem is that i'm writing this program under the C and linux :D

So I mean I want to produce something like this ("/tmp/2312") and use it as name for open function.... cause the number part is not static and it's being generating , so I have no idea how to use open() func with this variable. something like this ( open("/tmp."name ...) ) but I'll got the compile error
Use itoa() to convert integer to string and strcat() to glue together the 2 string pieces.
Or, use sprinf:

1
2
3
4
5
6
7

char Buffer[ 256 ];

sprintf( Buffer, "/tmp/%d", getpid() );

fildes = open( Buffer, "w" );
thanks so much
¿what about tmpfile ?
If you were going to use fopen anyway, I would go with ne555' suggestion:

http://www.cplusplus.com/reference/clibrary/cstdio/tmpfile/

But if you'd prefer to generate you're own file name, I would use a slightly longer file name for safety.

e.g.

sprintf( Buffer, "/tmp/myappname-%d", getpid() );
Topic archived. No new replies allowed.