Is it possible to load a list of words in an array from a text file?

2

Hello, as the question says, what I want to know is whether it is possible to load a list of words in an arrangement the way I am doing it. If someone can help me please here is a fragment of the code:

    #include<iostream>
    #include<synchapi.h>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    FILE *cargar_verbos; 
    int c_v=0;// este lo utilizo como contador
    string c__v; // este lo utilizo para cargar la palabra
    cargar_verbos=fopen("verbos_ar.txt","r"); 
    // el siguiente while lo utilizo para saber cuantas palabras hay en el              
    //archivo pues en este punto no tenemos ni idea.
    while(!feof(cargar_verbos))
    {
        fscanf(cargar_verbos,"%s",&c__v);
        c_v++; // por eso uso este contador

    }
    string verbos_ar[c_v]; //declaro el arreglo tipo string y utilizo el 
    //contador para declarar la cantidad



    c_v=0; // paso el contador nuevamente a cero

    while(!feof(cargar_verbos)) // aqui es donde tengo el problema
    {

        fscanf(cargar_verbos,"%s",&verbos_ar[c_v]); // esta es mi duda

        c_v++;

    }
    fclose(cargar_verbos);
    }

As you can see in the part that loads the word in the array I put the counter as an indicative and as I should store the word without problems but whenever I run the program and get to that part I get an error. If someone can help me, I would really appreciate it.

    
asked by metamax 16.03.2018 в 17:38
source

1 answer

1

First of all :

You are programming in C ++ (for the headers that you include, I imagine) but your code is C smooth and plain. Although C is compatible with C ++, the latter solves very common problems of the first practices, in addition to other things.

Going back to the problem :

In particular, as some comments say, in the second while you ask again if the end of the file is not reached, which will always be true given that the first loop did. Remember that each open file has an associated descriptor file and internal indexes that indicate in which byte of the file you are. The solution in this case is to use some line like:

fseek(cargar_verbos, 0, SEEK_SET)

or

rewind(cargar_verbos) 

Comment aside : Try to make the names of the variables good descriptions of the information they contain. cargar_verbos does not make sense, since that object does not load verbs, but contains them. A more favorable name would be, for example, archivo_verbos or lista_verbos .

Real solution: The correct solution, given that you are in C ++, is to use classes of the stdlib and C ++ syntax to solve it.

#include <iostream>    // Para imprimir por stdout
#include <fstream>     // Leer de archivo
#include <vector>      // Almacenar datos en un vector
#include <string>      // Para tener objetos string, o cadena de caracteres.

using namespace std; // Esto no es recomendable hacerlo siempre por el naming colision.

int main()
{
    auto success = EXIT_FAILURE;
    vector<string> vectorVerbos; // En C++ se usa convención camelCase

    ifstream archivoVerbos("verbos_ar.txt"); // Abrimos archivo
    if (archivoVerbos)  // Vemos si lo abrimos correctamente
    {
        string verbo;
        while (getline(archivoVerbos, verbo))
        {
            vectorVerbos.emplace_back(verbo);
        }
        archivoVerbos.close();
        success = EXIT_SUCCESS;
    }

    // Ahora vectorVerbos contiene todos los verbos.
    // Hacer lo necesario....

    return success;   
}
    
answered by 19.04.2018 / 15:30
source