-
December 28th, 2009, 10:44 PM
#1
need help with string
I have three variables:
const char* dir //directory of file
int i //the file's name without extension
const char* ext //the extension of the file
how do I add these together to get a const char* string that should look like this:
"img/1.bmp"
Thanks,
~justin123
-
December 28th, 2009, 10:48 PM
#2
Re: need help with string
sprintf would be one way.
-
December 28th, 2009, 10:54 PM
#3
Re: need help with string
would this work:
sprintf(name,"%d",dir+i+ext);
-
December 28th, 2009, 11:03 PM
#4
Re: need help with string
are you using C or C++??
In C++ you would use std::string and stringstreams.
In C you would use char* and sprintf. sprintf is pretty similar to printf except it prints to an area in memory and not the screen. If that sprintf were a printf, do you think it would print on the screen what you want?
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
December 28th, 2009, 11:08 PM
#5
Re: need help with string
Originally Posted by justin1232
would this work:
sprintf(name,"%d",dir+i+ext);
No.
-
December 28th, 2009, 11:10 PM
#6
Re: need help with string
im using c++ and i have no clue on how to do this.
-
December 28th, 2009, 11:47 PM
#7
Re: need help with string
the c++ way
Code:
#include <string>
#include <sstream>
template< class Type >
std::string ToString( Type Input )
{
std::stringstream Out;
Out << Input;
return Out.str();
}
using namespace std;
string File = ToString( Dir ) + ToString( I ) + ToString( Ext );
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
December 29th, 2009, 12:02 AM
#8
Re: need help with string
that part works, but how do i convert "file" to a const char* string so the function thats using it can understand it?
-
December 29th, 2009, 12:18 AM
#9
Re: need help with string
File.c_str();
http://www.cplusplus.com/reference/string/string/
is this function yours? if so, you should change it to
void Input( const string& File );
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|