I encountered a compilation error in Visual Studio 2015, that I am trying to convert char
data to LPWSTR
. Can I? Or does it work only with string types?
Here is a piece of my code :
⋮
FILE *sortie;
char fichier[256];// <--- HERE s my char table
int main(int argc, char *argv[])
{
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
GetCurrentDirectory(256, fichier);
strcat(fichier, "\\fichierlog.txt");
Before posting my question I was at:
I didn't find my case :(
Instead of your current code:
FILE *sortie;
char fichier[256];// <--- HERE s my char table
int main(int argc, char *argv[])
{
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
GetCurrentDirectory(256, fichier);
strcat(fichier, "\\fichierlog.txt");
do eg
auto main() -> int
{
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
std::wstring fichier( MAX_PATH, L'\0' );// <--- HERE s my char table
const DWORD len = GetCurrentDirectory( fichier.size(), &fichier[0] );
if( len == 0 || len >= fichier.size() ) { throw std::runtime_error( "GetCurrentDirectory failed." ); }
fichier.resize( len );
fichier += L"/fichierlog.txt";
std::ifstream sortie( fichier );
This should fix three issues:
You're compiling as Unicode (probably a Visual Studio project), but the code is for the Windows ANSI API.
You're using a C++ compiler, but the code is low level C.
Too small buffer for maximum path length, and possible buffer overrun for the concatenation.
Note that the ifstream
constructor that accepts a wide string is a Microsoft extension. It will however be practically required for Windows C++ compilers by the file system addition to the standard library in C++17.
You are compiling with unicode, so you have to use wchar_t
to declare the strings. Instead of strcat
use the unicode version which is wcscat
.
Also change the strings "\\fichierlog.txt" become L "\\fichierlog.txt"
FILE *sortie;
//char fichier[256];// <--- HERE s my char table
wchar_t fichier[256];// <--- HERE s my char table
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
GetCurrentDirectory(256, fichier);
//strcat(fichier, "\\fichierlog.txt");
wcscat(fichier, L"\\fichierlog.txt");
Your Visual Studio project is set to compile using "widechars" as default encoding (aka UNICODE), so all Windows APIs take wchar_t
arrays instead of char
arrays when handling strings.
Either set your project to use standard charset or specify the ASCII version of GetCurrentDirectory
by using GetCurrentDirectoryA
instead.
GetCurrentDirectory
is actually not a function, but a pre-processor macro that will route you to GetCurrentDirectoryA
or GetCurrentDirectoryW
depending on what enconding your compiler is set to use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.