We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

String to char* conversion

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:

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:

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.

7
Contributors
9
Replies
3 Years
Discussion Span
3 Years Ago
Last Updated
11
Views
Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
Skill Endorsements: 0

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.

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9
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 )
--------------------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

Eddy Dean
Junior Poster in Training
56 posts since Jul 2006
Reputation Points: 38
Solved Threads: 3
Skill Endorsements: 0

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;

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9

You shouldn't have to bother with all that, since InternetOpenUrl expects LPCTSTR (typedef as const TCHAR*) as its second argument. Try:

hFile = InternetOpenUrl( hINet, sUrl.c_str(), NULL, 0, 0, 0 );
GloriousEremite
Junior Poster in Training
65 posts since Jul 2006
Reputation Points: 108
Solved Threads: 14
Skill Endorsements: 0

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27

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:

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:

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.

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

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

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

leme know if u have any comments..

emagnun
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The easy way would be to have url as a char*. Or use different library. Microsoft library for strings has got the method to convert from string to char*, so if you would use it instead, you could convert your type by calling this method.

kudresov
Newbie Poster
19 posts since Dec 2009
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

here it is

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;

gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);

for(i=0;i<=l2;i++)
{
str1[i]=str2[i];
}
cout<<str1<<endl;
}
saba tabassum
Newbie Poster
2 posts since Jan 2010
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

What an appalling piece of crap!
"void main" and using"gets()" - sheesh.
Not to mention the lack of code tags, the use of obsolete headers yada yada yada.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0874 seconds using 2.69MB