This is a basic understanding concepts related question.
Working using: Embarcadero C++ Builder
What is the difference between:
opendir("C:\\XYZ")
and
String file = "C:\\XYZ";
opendir(file);
Aren't both strings?
The first one works but the sexond gives me error:
E2034 Cannot convert Unicode String to ' const char*'
In a case where I take input from the user I can only pass a string. How do i pass the whole path?
first one is a const char*, second one is a std::string. The opendir function accepts only const char* in your case and thus cannot convert std::string to const char* on its own. you can get the function to work by opendir(file.c_str()); .
No. A String is not a char array. opendir needs a char array.
opendir() expects an 8bit narrow const char* as input. When you pass a narrow literal to opendir(), you are passing it a const char[], which implicitly degrades to const char*, and all is fine.
String is System::String, which is a typedef for System::UnicodeString, which is Embarcadero's UTF-16 encoded string class (similar to std::wstring, but with different semantics). When you pass a String to opendir(), you get a conversion error.
To pass a String value to opendir() (or any other function that expects char*), you need to first convert it to a System::AnsiString, and then use AnsiString::c_str() to get a char* from it, eg:
String file = "C:\\XYZ";
opendir(AnsiString(file).c_str());
Related
I am trying to pass a const char* to a windows SetThreadDescription() function.
int pthread_setname_np(HANDLE thread, const char* name) {
return SetThreadDescription(thread, name);
}
and I am getting an error
'HRESULT SetThreadDescription(HANDLE,PCWSTR)': cannot convert argument 2 from 'const char *' to 'PCWSTR'
Of course I've stumbled into this and checked the character set and it was already set to Use Multi-Byte Character Set. Is there a way to fix this?
edit: add properties
#someprogrammerdude provided the answer in a comment:
The SetThreadDescription function doesn't seem to exist in an "ASCII" (non-wide character) version. You need to convert the string to wide-character string and use that converted string in the call.
I am now using C++ to program a robot using PROS. Pros has a print function, which is taking in a const char*. Now, I'm using lvgl to create my own screen, and I want to replicate the print function. Like the printf() functions, I want it to include variadic params to do the %d effect (so it converts all the %? to the corresponding values). The problem now is about the conversions between functions. I wanted to make a convert function to convert a string and the variadic params into a complete string. I need to input is a string which is like "hey" and I'm unsure what the type name should be. I need to be able to get size, search in it for %ds but I need the function to return a const char* to pass onto the lvgl to pring on the screen. I am having a bad time trying to convert a string into an const char* for the out put of the convert function.
Also, I tried using the input type as a char*, and when I input a string like "hello" is says a error [ISO C++11 does not allow conversion from string literal to 'char ' [-Wwritable-strings]]. But instead, when is use a const char, the error disappears. Anyone knows why?
Thanks everyone for your kind help!
char* and const char* are two flavours of the same thing: C-style strings. These are a series of bytes with a NUL terminator (0-byte). To use these you need to use the C library functions like strdup, strlen and so on. These must be used very carefully as missing out on the terminator, which is all too easy to do by accident, can result in huge problems in the form of buffer-overflow bugs.
std::string is how strings are represented in C++. They're a lot more capable, they can support "wide" characters, or variable length character sets like UTF-8. As there's no NUL terminator in these, they can't be overflowed and are really quite safe to use. Memory allocation is handled by the Standard Library without you having to pay much attention to it.
You can convert back and forth as necessary, but it's usually best to stick to std::string inside of C++ as much as you can.
To convert from C++ to C:
std::string cppstring("test");
const char* c_string = cppstring.c_str();
To convert from C to C++:
const char* c_string = "test";
std::string cppstring(c_string);
Note you can convert from char* (mutable) to const char* (immutable) but not in reverse. Sometimes things are flagged const because you're not allowed to change them, or that changing them would cause huge problems.
You don't really have to "convert" though, you just use char* as you would const char*.
std::string A = "hello"; //< assignment from char* to string
const char* const B = A.c_str(); //< call c_str() method to access the C string
std::string C = B; //< assignment works just fine (with allocation though!)
printf("%s", C.c_str()); //< pass to printf via %s & c_str() method
I'm using a function to download a file.
void downloadFile(const char* url, const char* fname) {
//..
}
This is called like :
downloadFile("http://servera.com/file.txt", "/user/tmp/file.txt");
This working fine.
But I want to change the URL to be a value from an array. The array stores encrypted values which when decrypted are strings, so I get the issue error: cannot convert ‘std::basic_string<char>’ to ‘const char*’
I've tried:
string test = decode(foo[5]);
const char* t1= test.c_str();
downloadFile(t1 "filename.txt", "/user/tmp/file.txt");
downloadFile(t1 + "filename.txt", "/user/tmp/file.txt");
and
downloadFile((decode(foo[5]).c_str()) + "filename.txt", "/user/tmp/file.txt");
which gives:
error: invalid operands of types ‘const char*’ and ‘const char [17]’ to binary ‘operator+’
What am I doing wrong ?
Thanks
C-strings can't be concatenated with +.
Use std::string::+ instead:
downloadFile((test + "filename.txt").c_str(), "/user/tmp/file.txt");
Note that c_str only returns a pointer to the std::string's internal character array, so it's valid only during the execution of the downloadFile function.
Try this:
downloadFile((decode(foo[5]) + "filename.txt").c_str(), "/user/tmp/file.txt");
The operator+ is not defined for char arrays.
The main problem in your code is that you are trying to use operator+ to concatenate raw C strings (i.e. raw const char* pointers, or raw char [] arrays), which doesn't work.
In C, you should use proper library functions (like strncat or safer variants) to do that; but since you are using C++, you can do better, and write easier code: just use a C++ string class, like std::string.
In fact, the C++ standard library offers convenient overloads for operator+ that work with std::string, so you can concatenate C++ strings in an easy, intuitive and safe way; for example:
// Build your URL string
std::string test = decode(foo[5]);
std::string url = test + "filename.txt";
// Use std::string::c_str() to convert from C++ string
// to C raw string pointer const char*
downloadFile(url.c_str(), "/user/tmp/file.txt");
I am trying to convert a const char pointer to a constant wide string pointer. Here is my code.
double ret;
const wchar_t* file;
file = const wchar_t* (file_old);
I get something that says this: "Error, type name not allowed." over the const.
Any help is greatly appreciated!
You need to actually convert the contents to which the pointer points to wide characters. The easiest way to do that is probably to put the narrow version into a string and convert using wstring_convert as detailed in this answer: https://stackoverflow.com/a/18374698/82320
I have a variable tweet that is a string and it has a character at the very beginning that I want to clip off.
So what I want to do is use strstr() to remove it. Here's my code:
tweet = strstr(tweet, "]");
However, I get this error:
cannot convert 'String' to 'const char*' for argument '1' to
'char' strstr(const char*, const char*)
So my thought would be to convert tweet into a char. How would I go about doing so?
string has a c_str() member function that returns const char *.
How about you use substring instead. This will be less confusing than converting between different types of string.
http://arduino.cc/en/Reference/StringSubstring
you can do that easier. Since you're using C++:
tweet = tweet.substring(1);
substr() returns a part of the string back to you, as string.
The parameter is the starting point of this sub string.
Since string index is 0-based, 1 should clip off the first character.
If you want to use strstr you can just cast tweet into a c-string:
tweet = strstr( tweet.c_str(), "]" );
However, that's pretty inefficient since it returns a c-string which has to be turned into a std::string against in order to fit into tweet.
I realize this is an old question, but if you're trying to, say, compare a specific char, and not just one letter in a string, then what you want is string.charAt(n). For example, if you're doing serial programming and you need to check for STX (\02) than you can use the following code.
char STX = '\02'
if (inputString.charAt(0) == STX) {
doSomething();
}
Using the following statement tweet.c_str() will return the string buffer, which will allow you to perform the edit you want.
Look at:
string.indexOf(val)
string.indexOf(val, from)
Parameters
string: a variable of type String
val: the value to search for - char or String
from: the index to start the search from
See this page