//prend en paramètre le chemin vers le fichier et le score // enregistre seulement si supérieur au meilleur score void sauverMeilleurScore(QString fichier_nom,int score) { // Création d'un objet QFile QFile file(fichier_nom); // On ouvre notre fichier en lecture seule et on vérifie l'ouverture if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() <<fichier_nom; }// Création d'un objet QTextStream à partir de notre objet QFile QTextStream flux(&file); //récupération du meilleur score dans le fichier int score_fichier; flux >> score_fichier; file.close(); QFile file_w(fichier_nom); file_w.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file_w); // écriture du score actuel seulement s'il est meilleur que le précédent score enregistré qWarning() << QString::number(score); if (score > score_fichier) out << score; // optional, as QFile destructor will already do it: file_w.close(); }
BOOL GetOpenFileNameU8Core(LPOPENFILENAME ofn, BOOL (WINAPI *ofn_func)(OPENFILENAMEW*)) { OPENFILENAMEW ofn_w; Wstr filter_w(MAX_PATH), cfilter_w(ofn->nMaxCustFilter), file_w(ofn->nMaxFile), ftitle_w(ofn->nMaxFileTitle), idir_w(MAX_PATH), title_w(MAX_PATH), defext_w(MAX_PATH), template_w(MAX_PATH); memcpy(&ofn_w, ofn, sizeof(OPENFILENAME)); WCHAR *wp=filter_w.Buf(); for (const char *p=ofn->lpstrFilter; p && *p; p+=strlen(p)+1) { wp += U8toW(p, wp, (int)(MAX_PATH - (wp - filter_w.Buf()))) + 1; } *wp = 0; U8toW(ofn->lpstrCustomFilter, cfilter_w.Buf(), ofn->nMaxCustFilter); U8toW(ofn->lpstrFile, file_w.Buf(), ofn->nMaxFile); U8toW(ofn->lpstrFileTitle, ftitle_w.Buf(), ofn->nMaxFileTitle); U8toW(ofn->lpstrInitialDir, idir_w.Buf(), MAX_PATH); U8toW(ofn->lpstrTitle, title_w.Buf(), MAX_PATH); U8toW(ofn->lpstrDefExt, defext_w.Buf(), MAX_PATH); U8toW(ofn->lpTemplateName, template_w.Buf(), MAX_PATH); if (ofn->lpstrFilter) ofn_w.lpstrFilter = filter_w.s(); if (ofn->lpstrCustomFilter) ofn_w.lpstrCustomFilter = cfilter_w.Buf(); if (ofn->lpstrFile) ofn_w.lpstrFile = file_w.Buf(); if (ofn->lpstrFileTitle) ofn_w.lpstrFileTitle = ftitle_w.Buf(); if (ofn->lpstrInitialDir) ofn_w.lpstrInitialDir = idir_w.s(); if (ofn->lpstrTitle) ofn_w.lpstrTitle = title_w.s(); if (ofn->lpstrDefExt) ofn_w.lpstrDefExt = defext_w.s(); if (ofn->lpTemplateName) ofn_w.lpTemplateName = template_w.s(); BOOL ret = ofn_func(&ofn_w); if (ofn->lpstrCustomFilter) { WtoU8(cfilter_w.s(), ofn->lpstrCustomFilter, ofn->nMaxCustFilter); } if (ofn->lpstrFileTitle) { WtoU8(ftitle_w.s(), ofn->lpstrFileTitle, ofn->nMaxFileTitle); } if (ofn->lpstrFile) { if (ofn_w.Flags & OFN_ALLOWMULTISELECT) { const WCHAR *wp=file_w.s(); char *p; for (p=ofn->lpstrFile; wp && *wp; wp+=wcslen(wp)+1) { p += WtoU8(wp, p, (int)(ofn->nMaxFile - (p - ofn->lpstrFile))) + 1; } *p = 0; } else { WtoU8(file_w.s(), ofn->lpstrFile, ofn->nMaxFile); } } // if (ofn_w.lpstrFile[ofn_w.nFileOffset]) ofn->nFileOffset = ::WideCharToMultiByte(CP_UTF8, 0, ofn_w.lpstrFile, ofn_w.nFileOffset, 0, 0, 0, 0); return ret; }
BOOL ShellExecuteExU8(SHELLEXECUTEINFO *info) { SHELLEXECUTEINFOW info_w; memcpy(&info_w, info, sizeof(SHELLEXECUTEINFO)); Wstr verb_w(info->lpVerb), file_w(info->lpFile), param_w(info->lpParameters), dir_w(info->lpDirectory), class_w(info->lpClass); info_w.lpVerb = verb_w.s(); info_w.lpFile = file_w.s(); info_w.lpParameters = param_w.s(); info_w.lpDirectory = dir_w.s(); info_w.lpClass = class_w.s(); BOOL ret = ::ShellExecuteExW(&info_w); info_w.lpFile = (WCHAR *)info->lpFile; info_w.lpParameters = (WCHAR *)info->lpParameters; info_w.lpDirectory = (WCHAR *)info->lpDirectory; info_w.lpClass = (WCHAR *)info->lpClass; memcpy(info, &info_w, sizeof(SHELLEXECUTEINFO)); return ret; }
HINSTANCE ShellExecuteU8(HWND hWnd, LPCSTR op, LPCSTR file, LPSTR params, LPCSTR dir, int nShow) { Wstr op_w(op), file_w(file), params_w(params), dir_w(dir); return ::ShellExecuteW(hWnd, op_w.s(), file_w.s(), params_w.s(), dir_w.s(), nShow); }