1
I’m taking words from a text file, and when it comes to printing them it’s all right, except when I store them in a vector. If I store them in a vector, at the time of printing, before the words come out some random characters.
void leArquivo(FILE* f, int numPalavras){
char palavra[30];
int flag = 0, i = 0, terminou = 0, cont = 0;
char** vetorDesord = malloc(sizeof(char)*(numPalavras-1));
while(!terminou){
flag = fscanf(f, "%s", palavra);
if (flag != EOF){
removeChar(palavra, '.', ',', ' ', ';', '"');
palavra[0] = tolower(palavra[0]);
vetorDesord[i] = malloc(sizeof(char)*strlen(palavra));
vetorDesord[i] = palavra;
printf("%s", palavra);
i++;
}
else terminou = 1;
cont++;
}
I’ve already tested:
- Without storing in vector, prints all correct.
- Without the functions
tolower()
andremoveChar()
, the error remains the same - Storing in the vector, anything I print in this excerpt (ex:
printf("a")
) also prints with error - I used
strcpy()
, the error persisted
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero