Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MSVC_example_fscanf_s_and_chars_wchars
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include "stdio.h" #include "wchar.h" // provides e.g. swprintf #if 0 #define bufsize 4u #else #define bufsize 16u #endif #define string1 "abcdefgh" #define string2 "ijklmnop" #define L_string1 L"abcdefgh" #define L_string2 L"ijklmnop" static void write_to_file (const char * file_name) { FILE * hFile; std::cout << "====" << __FUNCTION__ << "====" << std::endl; errno_t eError = fopen_s (&hFile, file_name, "w"); if (eError == 0) { int iCharsWritten = 0; printf ("writing this data: %u %d %c %C %s %S\n", 123u, -456, 'c', L'd', string1, L_string2); // write as chars to the screen printf ("as chars and as wchars\n"); iCharsWritten = fprintf (hFile, "%u %d %c %C %s %S\n", 123u, -456, 'c', L'd', string1, L_string2); // write as chars std::cout << "Write to file as char. chars written = " << iCharsWritten << std::endl; iCharsWritten = fwprintf (hFile, L"%u %d %c %C %s %S\n", 123u, -456, L'c', 'd', L_string1, string2); // write as wchars std::cout << "Write to file as wchar. chars written = " << iCharsWritten << std::endl; fclose (hFile); } else { std::cout << "fopen_s for create new file: FAILED!!! error = " << eError << std::endl; } } static void read_from_file_as_string (const char * file_name) /* do not use binary mode - else you might see even '\n' chars! */ { FILE * hFile; std::cout << "====" << __FUNCTION__ << "====" << std::endl; errno_t eError = fopen_s (&hFile, file_name, "r"); if (eError == 0) { int iResult = 0; { char ac[bufsize] = "0"; std::cout << "_countof(ac) = " << _countof(ac) << std::endl; /* bufsize */ std::cout << " sizeof(ac) = " << sizeof(ac) << std::endl; /* bufsize */ /* char */ std::cout << "char:\n"; for (size_t n = 0; n < 6; n++) { strcpy_s (ac, "0"); iResult = fscanf_s (hFile, "%s", ac, _countof(ac)); std::cout << "Read from file. fscanf_s result = " << iResult << "; retrived value: " << ac << std::endl; } } { wchar_t aw[bufsize] = L"0"; std::cout << "_countof(aw) = " << _countof(aw) << std::endl; /* bufsize */ std::cout << " sizeof(aw) = " << sizeof(aw) << std::endl; /* 128 */ /* wchar */ std::cout << "wchar:\n"; for (size_t n = 0; n < 6; n++) { wcscpy_s (aw, L"0"); iResult = fwscanf_s (hFile, L"%s", aw, _countof(aw)); std::wcout << L"Read from file. fwscanf_s result = " << iResult << L"; retrived value: " << aw << std::endl; /* wchars must be printed using wcout */ } } fclose (hFile); } else { std::cout << "fopen_s for read from file: FAILED!!! error = " << eError << std::endl; } } static void read_from_file_native (const char * file_name) { FILE * hFile; std::cout << "====" << __FUNCTION__ << "====" << std::endl; errno_t eError = fopen_s (&hFile, file_name, "r"); /* do not use binary mode - else you might see even '\n' chars! */ if (eError == 0) { unsigned int u = 0u; signed int s = 0; char c = '0'; wchar_t d = L'0'; char ac[bufsize] = "0"; wchar_t aw[bufsize] = L"0"; int iResult = 0; std::cout << "_countof(ac) = " << _countof(ac) << std::endl; /* bufsize */ std::cout << " sizeof(ac) = " << sizeof(ac) << std::endl; /* bufsize */ std::cout << "_countof(aw) = " << _countof(aw) << std::endl; /* bufsize */ std::cout << " sizeof(aw) = " << sizeof(aw) << std::endl; /* 128 */ /* char */ std::cout << "char:\n"; iResult = fscanf_s (hFile, "%u", &u); std::cout << "Read from file. fscanf_s result = " << iResult << "; retrived value: " << u << std::endl; iResult = fscanf_s (hFile, "%d", &s); std::cout << "Read from file. fscanf_s result = " << iResult << "; retrived value: " << s << std::endl; iResult = fscanf_s (hFile, " %c", &c); /* please note the leading space in the format string */ std::cout << "Read from file. fscanf_s result = " << iResult << "; retrived value: " << c << std::endl; iResult = fscanf_s (hFile, " %C", &d); /* please note the leading space in the format string */ std::wcout << L"Read from file. fscanf_s result = " << iResult << L"; retrived value: " << d << std::endl; /* wchars must be printed using wcout */ iResult = fscanf_s (hFile, "%s", ac, _countof(ac)); /* without the count we will likely see a segementation fault inside the function call */ std::cout << "Read from file. fscanf_s result = " << iResult << "; retrived value: " << ac << std::endl; iResult = fscanf_s (hFile, "%S", aw, _countof(aw)); /* without the count we will likely see a segementation fault inside the function call */ /* the value in the file is stored using only char width! */ std::wcout << L"Read from file. fscanf_s result = " << iResult << L"; retrived value: " << aw << std::endl; /* wchars must be printed using wcout */ /* wchar */ u = 0u; s = 0; c = '0'; d = L'0'; strcpy_s (ac, "0"); wcscpy_s (aw, L"0"); std::cout << "wchar:\n"; iResult = fwscanf_s (hFile, L"%u", &u); std::cout << "Read from file. fwscanf_s result = " << iResult << "; retrived value: " << u << std::endl; iResult = fwscanf_s (hFile, L"%d", &s); std::cout << "Read from file. fwscanf_s result = " << iResult << "; retrived value: " << s << std::endl; iResult = fwscanf_s (hFile, L" %C", &c); /* please note the leading space in the format string */ std::cout << "Read from file. fwscanf_s result = " << iResult << "; retrived value: " << c << std::endl; iResult = fwscanf_s (hFile, L" %c", &d); /* please note the leading space in the format string */ std::wcout << L"Read from file. fwscanf_s result = " << iResult << L"; retrived value: " << d << std::endl; /* wchars must be printed using wcout */ iResult = fwscanf_s (hFile, L"%S", ac, _countof(ac)); /* without the count we will likely see a segementation fault inside the function call */ /* the value in the file is stored using the much wider wchars! */ std::cout << "Read from file. fwscanf_s result = " << iResult << "; retrived value: " << ac << std::endl; iResult = fwscanf_s (hFile, L"%s", aw, _countof(aw)); /* without the count we will likely see a segementation fault inside the function call */ std::wcout << L"Read from file. fwscanf_s result = " << iResult << L"; retrived value: " << aw << std::endl; /* wchars must be printed using wcout */ fclose (hFile); } else { std::cout << "fopen_s for read from file: FAILED!!! error = " << eError << std::endl; } } int main(int argc, char * argv[]) { (void)argc; (void)argv; std::cout << "Hello, world!\n"; const char file_name[] = "helper.txt"; write_to_file (file_name); read_from_file_as_string (file_name); read_from_file_native (file_name); return 0; } /* sample outputs: Hello, world! ====write_to_file==== writing this data: 123 -456 c d abcdefgh ijklmnop as chars and as wchars Write to file as char. chars written = 31 Write to file as wchar. chars written = 31 ====read_from_file_as_string==== _countof(ac) = 16 sizeof(ac) = 16 char: Read from file. fscanf_s result = 1; retrived value: 123 Read from file. fscanf_s result = 1; retrived value: -456 Read from file. fscanf_s result = 1; retrived value: c Read from file. fscanf_s result = 1; retrived value: d Read from file. fscanf_s result = 1; retrived value: abcdefgh Read from file. fscanf_s result = 1; retrived value: ijklmnop _countof(aw) = 16 sizeof(aw) = 32 wchar: Read from file. fwscanf_s result = 1; retrived value: 123 Read from file. fwscanf_s result = 1; retrived value: -456 Read from file. fwscanf_s result = 1; retrived value: c Read from file. fwscanf_s result = 1; retrived value: d Read from file. fwscanf_s result = 1; retrived value: abcdefgh Read from file. fwscanf_s result = 1; retrived value: ijklmnop ====read_from_file_native==== _countof(ac) = 16 sizeof(ac) = 16 _countof(aw) = 16 sizeof(aw) = 32 char: Read from file. fscanf_s result = 1; retrived value: 123 Read from file. fscanf_s result = 1; retrived value: -456 Read from file. fscanf_s result = 1; retrived value: c Read from file. fscanf_s result = 1; retrived value: d Read from file. fscanf_s result = 1; retrived value: abcdefgh Read from file. fscanf_s result = 1; retrived value: ijklmnop wchar: Read from file. fwscanf_s result = 1; retrived value: 123 Read from file. fwscanf_s result = 1; retrived value: -456 Read from file. fwscanf_s result = 1; retrived value: c Read from file. fwscanf_s result = 1; retrived value: d Read from file. fwscanf_s result = 1; retrived value: abcdefgh Read from file. fwscanf_s result = 1; retrived value: ijklmnop */
run
|
edit
|
history
|
help
0
vector destruction - visual studio
Overload resolve function pointer
Preserving strict aliasing Union example
xyz1_1 programm
Rounding in C++
algorithm_1
Constant table but dynamic initialization at runtime
VC++ latest rejects istream to nullptr comparison
assaa
define_xml_tags