This is one of the easiest way to convert a std::string to std::wstring and vice versa:
1. Convert string to wstring:
std::string myString = " This is test string";
std::wstring wideString( myString.begin(), myString.end());
or
std::string myString = " This is test string";
std::wstring wideString;
wideString.assign( myString.begin(), myString.end());
2. Convert wstring to string - Similar to converting string to wstring
std::string wideString= L" This is wide string";
std::wstring asciiString( wideString.begin(), wideString.end());
or
std::string wideString= L" This is test string";
std::wstring asciiString;
asciiString.assign( wideString.begin(), wideString.end());
3. convert wstring to integer
int myInt;
wstring myString;
myInt = wcstol( myString.c_str(), NULL, 10);
read more about this function here: http://www.cplusplus.com/reference/cwchar/wcstol/.
Thứ Tư, 15 tháng 5, 2013
[C/C++] Check if a file exists or not
you can use fopen("file_name", "w+"); to check if a file exist or not.
For more checking option, such as check access level you can use
access()
, e.g.:#include <unistd.h>
int res = access(path, R_OK);
if (res < 0) {
if (errno == ENOENT) {
// file does not exist
} else if (errno == EACCES]) {
// file exists but is not readable
} else {
// uh oh
}
}
Đăng ký:
Bài đăng (Atom)