1
I have a file dados.txt
with (fictitious) Cpfs in the following format:
382031758-71
647900189-01
460754503-73
696170135-72
And so on, being in total 500 cpfs. I’m trying to read each and put in char cpf[12]
(because each one has 12 characters counting the -
), but when printing are printed three strange characters type @ýý
int main(){
//abre o arquivo em modo leitura
FILE *dados = fopen("dados.txt", "r");
char cpf[12];
fseek(dados, 0, SEEK_SET); //vai para o inicio do arquivo
//fgets(cpf, 100, dados); //pega 12 caracteres
for(int i = 0; i < 12; i++){
cpf[i] = fgetc(dados);
}
printf("%s\n", cpf);
fclose(dados);
}
I also tried with fscanf(dados, "%s\n", cpf);
but gave in anyway. So I would like to understand how to read this data in this way. I want to store in a variable because I need to use this to test a hash function later.
I believe it is because your vector is not considering the end of the string, change
char cpf[12]
forchar cpf[13]
– Thiago Fernandes
that’s not it, even if it changes to Cpf[100] continues like this
– Leila
C or C++ ? The code you have is C but marked the tag C++ and C++ has much simpler ways to read file information, as well as to store in variables.
– Isac
whatever, it can be in c++
– Leila