Hello, as the question says, what I want to know is if it is possible to load a list of words in an array the way I am doing it. If someone can help me please here is a snippet 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 indicative and according to me it should store the word without problems but whenever I run the program and it reaches that part it gives me an error. If anyone can help me I would really appreciate it.
First of all :
You're programming in C++ (because of the headers you include, I guess) but your code is plain old C. Although C is compatible with C++, the latter solves very common problems of the former's practices, among other things.
Back to the problem :
Specifically, as some comments say, in the second while you ask again if the end of the file was not reached, which will always be true since the first loop did. Remember that each open file has an associated file descriptor and internal indexes that indicate which byte of the file you are in. The solution in this case is to use some line like:
either
Side Comment : Try to make variable names good descriptions of the information they contain.
cargar_verbos
doesn't make sense, since that object doesn't load verbs, but contains them. A more suitable name would be, for example,archivo_verbos
orlista_verbos
.Actual solution: The correct solution, since you are in C++, is to use C++ classes
stdlib
and syntax to solve it.