1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
//Ceci est le code du site cité plus haut
/**
* Convert a command line parameter, provided as a single character
* format, into a wide character format.
*
* @param[in] int argc Number of parameters available on the command line.
* @param[in] char *argv[] Array of pointers to command line values.
* @param[out] wchar_t **dst Destination wide character string.
* @param[in] int index Command line parameter index to format as wide string.
* @return wchar_t *dst Destination wide character string.
* If the specified command line parameter is missing an empty string will be reported.
*/
wchar_t *AomCommandLineW(int argc, char *argv[], wchar_t **dst, int index) {
wchar_t *dst_p;
int i = 0;
int i_max = 0;
//dst_p will hold the address of our wide character array.
//dst will hold the address of the parameter to redirect to the wide character array.
dst_p = *dst;
// Report a blank wchar_t string on error or null parameter.
if ((index >= argc) || (index < 0) || (!argc) || (!argv)) {
free(dst_p);
dst_p = static_cast<wchar_t*>(malloc(sizeof(wchar_t)));
//std::fill(dst_p, dst_p + sizeof(wchar_t), 0);
memset(dst_p, 0, sizeof(wchar_t));
*dst = dst_p;
return dst_p;
}
i_max = strlen(argv[index]);
// Free and re-allocate destination memory. Don't forget the terminating null.
free(dst_p);
dst_p = static_cast<wchar_t*>(malloc((strlen(argv[index]) * sizeof(wchar_t)) + sizeof(wchar_t)));
// Copy the string, converting each character as we go. Don't forget the terminating null.
for (i = 0; i < i_max; i++) {
mbtowc(&dst_p[i], &argv[index][i], sizeof(char));
}
memset(&dst_p[i], 0, sizeof(wchar_t));
//Update the destination argument.
*dst = dst_p;
return dst_p;
}
int main(int argc, char **argv) {
//Utilisation de la bonne locale
setlocale( LC_ALL, "" );
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " file";
exit(1);
}
//Conversion char* en wchar_t*
wchar_t *dst = NULL;
AomCommandLineW(argc, argv, &dst, 1);
std::wstring filename = dst;
//Recopie du wstring dans un string "classique"
std::string t(filename.begin(), filename.end());
//Utilisation du string pour ouvrir le fichier
std::wifstream wfs(t.c_str());
std::wstring temp;
if (!wfs.is_open())
{
std::cerr << "Error!!!";
}
while (!wfs.eof()) {
wfs >> temp;
std::wcout << temp;
}
wfs.close();
} |
Partager