1
Hello, I’m having a problem to skip lines in a file, I use fgets(), but it always prints the first line of my file.
I also did some tests, and for some reason, there is only one iteration and x enters the if with the value of Random.
Note: each line in the file is always < 30 characters.
void randInventario() {
srand(time(NULL));
int random = rand() % 10;
nome = new char;
desc = new char;
strcpy(parm,"armas.txt");
FILE* arquivo = fopen(parm,"rt");
if (arquivo == NULL) {
cout << "Não foi possível abrir o arquivo." << "\n" << endl;
}
int x = 0;
char text[30];
while(!feof(arquivo))
{
if(x = random)
{
fscanf(arquivo, "%s %d %s\n", nome, &dano, desc);
break;
}
fgets(text,30,arquivo);
++x;
}
fclose(arquivo);
}
Since you are using C++, do not use
fgets
,fscanf
,fopen
, etc. Using streams can already be a big step to facilitate the work.– Maniero