Problem with string storage and printing in C

Asked

Viewed 103 times

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:

  1. Without storing in vector, prints all correct.
  2. Without the functions tolower() and removeChar(), the error remains the same
  3. Storing in the vector, anything I print in this excerpt (ex: printf("a")) also prints with error
  4. 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).

1 answer

1

It has several errors and the solution is more complex than precise and rather inefficient, besides escaping from the standard that is usually done in C.

We usually allocate the required memory where it needs and move on to the manipulate function. We avoid using enough malloc(), mainly because people forget to give free(), what seems to be the case. Even if you don’t want to pass the already allocated vector, it seems that you could allocate to its same function.

I wouldn’t do that because the function is reading and printing data, are separate things, this is usually considered wrong.

I didn’t write the function that removes characters, I hope it’s simple and correct, but I’m sure it’s not. And it is there that should already make the letters lowercase, besides being more efficient is correct, what is doing in your code is to use a function that converts a character to try to do in the whole word, does not work, at most will do in the first character of the word.

Your code is problematic if you have more words than you expect. The loop control is too complex. And there are too many variables. There are other improvements to be made.

And if you’re gonna use the malloc() even, it makes no sense to use sizeof(char) because he’s always 1. And use strlen() it’s almost always a mistake. And then would have to use strcpy(), what is more an inefficiency.

#include <stdio.h>

int leDados(FILE* arquivo, int numPalavras, char palavras[numPalavras][31]) {
    for (int i = 0; i < numPalavras; i++) {
        if (fscanf(arquivo, "%30s", palavras[i]) == EOF) return i;
        //removeCharETornaMinuscula(palavra, '.', ',', ' ', ';', '"');
    }
    return 0;
}

int main(void) {
    char palavras[10][31];
    int numPalavras = leDados(stdin, 10, palavras);
    for (int i = 0; i < numPalavras; i++) printf("%s\n", palavras[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.