String to char* conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

String to char* conversion

 
0
  #1
Jul 21st, 2006
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:

  1.  
  2. char filename[1024];
  3. char* url;
  4. string sUrl;
  5.  
  6.  
  7. HINTERNET hINet, hFile;
  8. hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
  9. if ( !hINet )
  10. {
  11. return false;
  12. }
  13. //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10
  14. int Page = 0;
  15. while(Page < 12)
  16. {
  17. sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world=";
  18. sUrl+=Server;
  19. sUrl+="&list=";
  20. sUrl+=Skill;
  21. sUrl+="&page=";
  22. sUrl+=Page;
  23. Page+=1;
  24.  
  25. url = sUrl.c_str();
  26. cout << url << endl;
  27.  
  28. hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 );
  29. //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:
  1. Compiling...
  2. TibiaName.cpp
  3. C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(169) : error C2440: '=' : cannot convert from 'const char *' to 'char *'
  4. Conversion loses qualifiers
  5. 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 *'
  6. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
  7. Error executing cl.exe.
  8.  
  9. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String to char* conversion

 
0
  #2
Jul 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: String to char* conversion

 
0
  #3
Jul 21st, 2006
  1. char* url;
  2. string sUrl;
  3.  
  4.  
  5. HINTERNET hINet, hFile;
  6. hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
  7. if ( !hINet )
  8. {
  9. return false;
  10. }
  11. //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10
  12. int Page = 0;
  13. while(Page < 12)
  14. {
  15. sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world=";
  16. sUrl+=Server;
  17. sUrl+="&list=";
  18. sUrl+=Skill;
  19. sUrl+="&page=";
  20. sUrl+=Page;
  21. Page+=1;
  22.  
  23. strcpy(url, sUrl.c_str());
  24. cout << url << endl;
  25.  
  26.  
  27. hFile = InternetOpenUrl( hINet, url, NULL, 0, 0, 0 );
  28. if ( hFile )

  1. --------------------Configuration: TibiaName - Win32 Debug--------------------
  2. Compiling...
  3. TibiaName.cpp
  4. C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(170) : warning C4700: local variable 'url' used without having been initialized
  5. Linking...
  6.  
  7. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String to char* conversion

 
0
  #4
Jul 21st, 2006
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;
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: String to char* conversion

 
0
  #5
Jul 21st, 2006
You shouldn't have to bother with all that, since InternetOpenUrl expects LPCTSTR (typedef as const TCHAR*) as its second argument. Try:
  1. hFile = InternetOpenUrl( hINet, sUrl.c_str(), NULL, 0, 0, 0 );
Last edited by GloriousEremite; Jul 21st, 2006 at 7:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: String to char* conversion

 
0
  #6
Jul 22nd, 2006
> 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
const char *url;
to satisfy the const requirements.

Or just do what GloriousEremite showed, and scrap the whole temporary used once thing.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: emagnun is an unknown quantity at this point 
Solved Threads: 0
emagnun emagnun is offline Offline
Newbie Poster
 
-1
  #7
3 Days Ago
Originally Posted by Eddy Dean View Post
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:

  1.  
  2. char filename[1024];
  3. char* url;
  4. string sUrl;
  5.  
  6.  
  7. HINTERNET hINet, hFile;
  8. hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
  9. if ( !hINet )
  10. {
  11. return false;
  12. }
  13. //http://www.tibia.com/statistics/?subtopic=highscores&world=Aldora&list=magic&page=10
  14. int Page = 0;
  15. while(Page < 12)
  16. {
  17. sUrl+="http://www.tibia.com/statistics/?subtopic=highscores&world=";
  18. sUrl+=Server;
  19. sUrl+="&list=";
  20. sUrl+=Skill;
  21. sUrl+="&page=";
  22. sUrl+=Page;
  23. Page+=1;
  24.  
  25. url = sUrl.c_str();
  26. cout << url << endl;
  27.  
  28. hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 );
  29. //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:
  1. Compiling...
  2. TibiaName.cpp
  3. C:\Program Files\Microsoft Visual Studio\MyProjects\TibiaName\TibiaName.cpp(169) : error C2440: '=' : cannot convert from 'const char *' to 'char *'
  4. Conversion loses qualifiers
  5. 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 *'
  6. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
  7. Error executing cl.exe.
  8.  
  9. 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.



------------------------------------------------

Hi instead of url=sUrl.c_str() u can write like

url=(char *)(sUrl.c_str())

leme know if u have any comments..
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC