Last active
March 18, 2022 17:30
-
-
Save fowlmouth/92ec07934fd29197734142b17ecef66c to your computer and use it in GitHub Desktop.
c unicode examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wchar.h> | |
#include <stdio.h> | |
#include <locale.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
setlocale(LC_CTYPE, ""); | |
FILE* f = fopen("test.string", "r"); | |
long int position = ftell(f); | |
wint_t character = fgetwc(f); | |
while(character != WEOF) | |
{ | |
printf("char: %lc position: %ld\n", character, position); | |
position = ftell(f); | |
character = fgetwc(f); | |
} | |
fclose(f); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALL: file string | |
string: string.o | |
cc -o $@ $< | |
file: file.o | |
cc -o $@ $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wchar.h> | |
#include <stdio.h> | |
#include <locale.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
setlocale(LC_CTYPE, ""); | |
const char *test_string = "A🍄B🌶C🍍"; | |
const char *test_string_end = test_string + strlen(test_string); | |
mbstate_t mbs; | |
mbsinit(&mbs); | |
for(const char* str = test_string; str < test_string_end; ) | |
{ | |
wchar_t wchar; | |
const int mbresult = mbtowc(&wchar, str, test_string_end - str); | |
printf("mbtowc result: %d char: %lc\n", mbresult, wchar); | |
if(mbresult <= 0) | |
break; | |
str += mbresult; | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A🍄B🌶C🍍 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment