I'm studying C and now I started to see how to manipulate files, that is: how to write data, read and delete it.
However I am experiencing a problem, my program will use accents and special characters, so I set setlocation with my language so that they work perfectly.
But it ends up creating two inconsistencies:
When I put words with accents to save in the file, and I try to display them on the screen, returns several strange characters. Note that accented words within the printf are displayed normally.
When I remove setlocation, the accented words inserted in the file are displayed perfectly, but the printf with accents is not displayed.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, "Portuguese");
FILE *my_file;
char name[90];
fopen_s(&my_file, "names.txt", "a");
printf("Digite uma palavra acentuada [não/ação/maldição]: ");
scanf_s("%[^\n]", name, 90);
fputs(name, my_file);
fputs("\n", my_file);
fclose(my_file);
fopen_s(&my_file, "names.txt", "r");
int i;
char *result;
char lines[100];
i = 1;
while (!feof(my_file))
{
result = fgets(lines, 100, my_file);
if (result)
printf("%s", lines);
i++;
}
fclose(my_file);
return 0;
}
Any suggestions on how to fix this? Thank you.