| | |
String to char* conversion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
Solved Threads: 3
Hello,
I've had some problems converting a string into a char. I've tried many, many things, but it didn't seem to work.
I'll post the code below:
As you can see, I'm making a string that will contain the URL, I place the URL in it and then want to convert it to a char*.
I really tried about everything.
The URL doesn't neccesarely have to be a string, but I thought that would be the easiest way.
The errors that I get are:
line 169 is
Greetz, Eddy
Edit: The error is in the strcpy() line, weird thing is that if I remove that line (put // before it) it still crashes. I will try to find out why.
I've had some problems converting a string into a char. I've tried many, many things, but it didn't seem to work.
I'll post the code below:
C++ Syntax (Toggle Plain Text)
char filename[1024]; char* url; string sUrl; HINTERNET hINet, hFile; hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if ( !hINet ) { return false; } //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10 int Page = 0; while(Page < 12) { sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world="; sUrl+=Server; sUrl+="&list="; sUrl+=Skill; sUrl+="&page="; sUrl+=Page; Page+=1; url = sUrl.c_str(); cout << url << endl; hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 ); //The rest of the loop, no troubles here.
As you can see, I'm making a string that will contain the URL, I place the URL in it and then want to convert it to a char*.
I really tried about everything.
The URL doesn't neccesarely have to be a string, but I thought that would be the easiest way.
The errors that I get are:
C++ Syntax (Toggle Plain Text)
Compiling... TibiaName.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(169) : error C2440: '=' : cannot convert from 'const char *' to 'char *' Conversion loses qualifiers C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(173) : error C2440: 'type cast' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. TibiaName.exe - 2 error(s), 0 warning(s)
line 169 is
url = sUrl.c_str(); and line 173 is hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 );Greetz, Eddy
Edit: The error is in the strcpy() line, weird thing is that if I remove that line (put // before it) it still crashes. I will try to find out why.
Last edited by Eddy Dean; Jul 21st, 2006 at 3:10 pm.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
url = sUrl.c_str();
This won't work because you can't assign one Cstyle string to another. At the top of your code add the line #include <cstring>
and then use this:
strcpy(url, sUrl.c_str());
for the above line. There are other rules for using C style strings. If you are going to be using them routinely you should probably find a good C/C++ book and learn about them They are also called null terminated char arrays.
This won't work because you can't assign one Cstyle string to another. At the top of your code add the line #include <cstring>
and then use this:
strcpy(url, sUrl.c_str());
for the above line. There are other rules for using C style strings. If you are going to be using them routinely you should probably find a good C/C++ book and learn about them They are also called null terminated char arrays.
•
•
Join Date: Jul 2006
Posts: 56
Reputation:
Solved Threads: 3
C++ Syntax (Toggle Plain Text)
char* url; string sUrl; HINTERNET hINet, hFile; hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if ( !hINet ) { return false; } //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10 int Page = 0; while(Page < 12) { sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world="; sUrl+=Server; sUrl+="&list="; sUrl+=Skill; sUrl+="&page="; sUrl+=Page; Page+=1; strcpy(url, sUrl.c_str()); cout << url << endl; hFile = InternetOpenUrl( hINet, url, NULL, 0, 0, 0 ); if ( hFile )
C++ Syntax (Toggle Plain Text)
--------------------Configuration: TibiaName - Win32 Debug-------------------- Compiling... TibiaName.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(170) : warning C4700: local variable 'url' used without having been initialized Linking... TibiaName.exe - 0 error(s), 1 warning(s)
and craaaaaash... The program crashes without returning any data.
I didn't find the error yet by breakpoints. I will try to continue the search..
Thanks anyway
Greetz, Eddy
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
Remember where I said it would be a good idea to read about C style strings if you were going to use them? This is probably an example why.
url is declared as a char *, however it probably never had any memory given to it to point to. Therefore, when you try to write to it using the strcpy() call, boom. You can either change url to a fixed size char array using static memory, and make it big enough to hold any likely string you will end up putting into it, or you can use dynamic memory to make url just the right size.
//using dynamic memory
char * url;
url = new char[sUlr.length() + 1];
strcpy(url, sUlr.c_str());
//remember free up memory for url before using it again or when done with it by calling:
delete[] url;
url is declared as a char *, however it probably never had any memory given to it to point to. Therefore, when you try to write to it using the strcpy() call, boom. You can either change url to a fixed size char array using static memory, and make it big enough to hold any likely string you will end up putting into it, or you can use dynamic memory to make url just the right size.
//using dynamic memory
char * url;
url = new char[sUlr.length() + 1];
strcpy(url, sUlr.c_str());
//remember free up memory for url before using it again or when done with it by calling:
delete[] url;
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
You shouldn't have to bother with all that, since InternetOpenUrl expects LPCTSTR (typedef as const TCHAR*) as its second argument. Try:
C++ Syntax (Toggle Plain Text)
hFile = InternetOpenUrl( hINet, sUrl.c_str(), NULL, 0, 0, 0 );
Last edited by GloriousEremite; Jul 21st, 2006 at 7:43 pm.
> char *url;
> url = sUrl.c_str();
Which got you
error C2440: '=' : cannot convert from 'const char *' to 'char *'
Understand that the c_str() method returns a const pointer for a reason, and that's to try and stop you from sneaking in the back door to change the string.
> char *url;
> strcpy(url, sUrl.c_str());
Which got you
warning C4700: local variable 'url' used without having been initialized
Yes, you used a variable before you initialised it.
Expect string data to be sprayed all over some unknown memory.
You can either allocate it as Lerner suggests,
Or simply have
to satisfy the const requirements.
Or just do what GloriousEremite showed, and scrap the whole temporary used once thing.
> url = sUrl.c_str();
Which got you
error C2440: '=' : cannot convert from 'const char *' to 'char *'
Understand that the c_str() method returns a const pointer for a reason, and that's to try and stop you from sneaking in the back door to change the string.
> char *url;
> strcpy(url, sUrl.c_str());
Which got you
warning C4700: local variable 'url' used without having been initialized
Yes, you used a variable before you initialised it.
Expect string data to be sprayed all over some unknown memory.
You can either allocate it as Lerner suggests,
Or simply have
const char *url;to satisfy the const requirements.
Or just do what GloriousEremite showed, and scrap the whole temporary used once thing.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
-1
#7 3 Days Ago
•
•
•
•
Hello,
I've had some problems converting a string into a char. I've tried many, many things, but it didn't seem to work.
I'll post the code below:
C++ Syntax (Toggle Plain Text)
char filename[1024]; char* url; string sUrl; HINTERNET hINet, hFile; hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if ( !hINet ) { return false; } //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10 int Page = 0; while(Page < 12) { sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world="; sUrl+=Server; sUrl+="&list="; sUrl+=Skill; sUrl+="&page="; sUrl+=Page; Page+=1; url = sUrl.c_str(); cout << url << endl; hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 ); //The rest of the loop, no troubles here.
As you can see, I'm making a string that will contain the URL, I place the URL in it and then want to convert it to a char*.
I really tried about everything.
The URL doesn't neccesarely have to be a string, but I thought that would be the easiest way.
The errors that I get are:
C++ Syntax (Toggle Plain Text)
Compiling... TibiaName.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(169) : error C2440: '=' : cannot convert from 'const char *' to 'char *' Conversion loses qualifiers C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(173) : error C2440: 'type cast' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. TibiaName.exe - 2 error(s), 0 warning(s)
line 169 isurl = sUrl.c_str();and line 173 ishFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 );
Greetz, Eddy
Edit: The error is in the strcpy() line, weird thing is that if I remove that line (put // before it) it still crashes. I will try to find out why.
------------------------------------------------
Hi instead of url=sUrl.c_str() u can write like
url=(char *)(sUrl.c_str())
leme know if u have any comments..
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Overloaded function
- Next Thread: DirectX 9 Stack Overflow.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






