I want to read line by line a text file and add each line in a array, I try something like that, but something is wrong with my array, what ?
QFile inputFile("C:\\pepoles.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
QString pepoles[1000];
while ( !in.atEnd() )
{
QString line = in.readLine();
pepoles[] = line;
}
ui->lineEdit->setText(pepoles[0]);
}
else{
QMessageBox::critical(this, "Ouups",
"Le fichier est introuvable ou vide...");
}
inputFile.close();
}
Thanks !
Keep track of the number of lines you've read, and index pepoles with it. Also, make sure you don't exceed your arrays capacity.
int lineNum = 0;
QFile inputFile("C:\\pepoles.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
QString pepoles[1000];
while ( !in.atEnd() && lineNum < 1000)
{
QString line = in.readLine();
pepoles[lineNum++] = line;
}