C ++ file import .txt

0

When I do the import a space is added at the beginning of the text. example: "A" How do I ignore it?

Texto::Texto() {
    string pal, arquivoNome;
    char *palavra_aux;
    int i = 0;
    char *token = NULL;

    cout<<"Digite o nome do arquivo que contém o texto a ser corrigo, com a extensão: ";
    getline(cin,arquivoNome);

    ifstream arquivo(arquivoNome);
    pal.assign((istreambuf_iterator<char>(arquivo)),(istreambuf_iterator<char>())); // Tranfosma o conteudo do do arquivo para char

    palavra_aux = new char[pal.length()+1];
    strcpy(palavra_aux, pal.c_str());

    texto = palavra_aux;

    if (arquivo.is_open()) {

        token = strtok(palavra_aux," ,.;-? ");

        while (token != NULL) {
            palavra[i] = new Palavra(token);
            i++;
            token = strtok (NULL, " ,.;-? ");
        }
    }
    else {
        cout << "Falha ao abrir o arquivo!" << endl;
    }

    arquivo.close(); // Fecha arquivo
    tam = i;
}
    
asked by anonymous 09.08.2018 / 21:08

1 answer

0

Your problem is on these lines

palavra_aux = new char[pal.length()+1];
strcpy(palavra_aux, pal.c_str());

You create the vector of char with the size of the string + 1 and then use the strcpy that will instantiate the string size to occupy the last positions of the vector, pal.length () vector positions and the one left over (in your case the first) will be a garbage, which in your case is showing a blank.

palavra_aux = new char[pal.length()];

length already returns the length of the string not the last position.

    
09.08.2018 / 22:15