I'm trying to create a container that holds the Japanese alphabet, so I'm using the data type wchar_t, and have my .c and .txt files encoded in UTF-8. The idea is that the program populates the container by reading in the .txt file. I can read in the first few lines, but after a few iterations the program segfaults. I suspect it is fgetws because of a few wprintf statements that I've put around the function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
typedef struct entry {
wchar_t romaji[5];
} entry;
typedef struct dictionary {
struct entry entry[46];
} dictionary;
void init_dictionary(dictionary **dictionary) {
(*dictionary) = malloc(sizeof(dictionary));
}
void add_romaji(dictionary *dictionary, wchar_t *romaji, int index) {
entry new;
wcscpy(new.romaji, romaji);
dictionary->entry[index] = new;
}
void populate(dictionary *dictionary) {
FILE *fp;
wchar_t line[5];
wchar_t *ptr;
wchar_t romaji[5];
int i;
fp = fopen("romaji.txt", "r");
i = 0;
while(fgetws(line, 5, fp) != NULL) {
wcstok(line, L"\n", &ptr);
wcscpy(romaji, line);
add_romaji(dictionary, romaji, i);
++i;
}
fclose(fp);
}
int main(int argc, char ** argv) {
setlocale(LC_CTYPE, "");
dictionary *dictionary;
init_dictionary(&dictionary);
populate(dictionary);
return EXIT_SUCCESS;
}
romaji.txt is in UTF-8, and just contains a line for each character in the Japanese alphabet. I'm unable to get the indentation here to work, so I cannot post it, sorry. I tried searching for an answer, but none have helped. If anyone could let me know what my mistake is, I would appreciate it.
Aucun commentaire:
Enregistrer un commentaire