Computing.Net > Forums > Programming > C++ const char conversion

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ const char conversion

Reply to Message Icon

Name: djas
Date: February 12, 2006 at 06:19:49 Pacific
OS: Windows XP Pro SP2
CPU/Ram: PM 1.73 GHZ 1GB RAM
Product: Gateway MX6635b
Comment:

hey guys,
can anyone tell me why my code won't work?
i think it is because i need to convert my string to const char.
i get this error.

[error]

cannot convert `std::string' to `const char*' in initialization

[/error]

my code is below

[code]
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
cin >> str;
const char *file = str, *hide = "test.txt";
CopyFile(file, hide, true);
cin.get();
return 0;
}

[/code]

djas



Sponsored Link
Ads by Google

Response Number 1
Name: saddam (by lurker)
Date: February 15, 2006 at 20:32:12 Pacific
Reply:

If I'm not wrong, change this line to read:

const char *file = str.c_str();

This method returns a pointer to a string.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ const char conversion

Error Executing www.computing.net/answers/programming/error-executing/8991.html

C++ Invalid Conversion and namespac www.computing.net/answers/programming/c-invalid-conversion-and-namespac/13224.html

Char Error in C++ www.computing.net/answers/programming/char-error-in-c/18600.html