QString vectorData[100000];
QString X[10000], Y[10000], infoNumber[10000], infoD[10000], infoShape[100];
Change all of these to QStringList or QVector<QString> and you will never have to worry about the size being too small.
void MainWindow::on_pushButtonCargarGerber_clicked()
{
QFile archivo
(nombreArchivo.
toUtf8().
constData());
int ii = 0;
int jj = 0;
qDebug()<<"No se puede abrir " << nombreArchivo;
exit(1);
}
else{
...
vectorData.clear(); // First easy change - ensure vectors start empty
infoNumeral.clear();
infoX.clear();
infoY.clear();
// ... etc.
while (!Texto.atEnd()){
vectorData.push_back( Texto.readLine() ); // Second easy change: vectorData will grow if needed
int posicionX = vectorData[ii].indexOf(separatorX);
...
int tamanioFila = vectorData[ii].length();
// ************************** COORDENADAS XY ************************** //
int tamXY;
if(F2.toInt()==4) tamXY=18; if(F2.toInt()==5) tamXY=20;
if(tamanioFila == tamXY){
QString temporal
= vectorData
[ii
].
mid(0, posicionX
+1);
if(temporal==separatorX){
temporal.
insert(0,
QString::number(jj
+1));
infoNumero.push_back( temporal ); // Next change
int c;
if(jj>=0 && jj<9) c = 1;
else if(jj>=9 && jj<99) c = 2;
else if(jj>=99 && jj<999) c = 3;
else c = 4;
temporal = infoNumero[jj].mid(0, c);
infoNumero[jj] = temporal;
// X
temporal = vectorData[ii].mid(posicionX+1, posicionY-1);
infoX.push_back( temporal ); // Next change
// Y
... // etc. for decoding other parameters
jj++;
}
}
ii++;
}
}
}
void MainWindow::on_pushButtonCargarGerber_clicked()
{
QString nombreArchivo = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath(), tr("Files (*.grb *.txt)"),0,QFileDialog::DontUseNativeDialog);
QFile archivo(nombreArchivo.toUtf8().constData());
QTextStream Texto(&archivo);
int ii = 0;
int jj = 0;
if(!archivo.open(QIODevice::ReadOnly | QFile::Text)){
qDebug()<<"No se puede abrir " << nombreArchivo;
exit(1);
}
else{
QString separatorX = "X";
...
vectorData.clear(); // First easy change - ensure vectors start empty
infoNumeral.clear();
infoX.clear();
infoY.clear();
// ... etc.
while (!Texto.atEnd()){
vectorData.push_back( Texto.readLine() ); // Second easy change: vectorData will grow if needed
int posicionX = vectorData[ii].indexOf(separatorX);
...
int tamanioFila = vectorData[ii].length();
// ************************** COORDENADAS XY ************************** //
int tamXY;
if(F2.toInt()==4) tamXY=18; if(F2.toInt()==5) tamXY=20;
if(tamanioFila == tamXY){
QString temporal = vectorData[ii].mid(0, posicionX+1);
if(temporal==separatorX){
temporal.insert(0, QString::number(jj+1));
infoNumero.push_back( temporal ); // Next change
int c;
if(jj>=0 && jj<9) c = 1;
else if(jj>=9 && jj<99) c = 2;
else if(jj>=99 && jj<999) c = 3;
else c = 4;
temporal = infoNumero[jj].mid(0, c);
infoNumero[jj] = temporal;
// X
temporal = vectorData[ii].mid(posicionX+1, posicionY-1);
infoX.push_back( temporal ); // Next change
// Y
... // etc. for decoding other parameters
jj++;
}
}
ii++;
}
}
}
To copy to clipboard, switch view to plain text mode
You are using "jj" to index several arrays - are each of these arrays exactly matched (in other words, when you decode a line, if it contains "X" will it also contain Y, infoNumber, and infoD? If not, then these vectors will not be synchonized - the entries will refer to different lines, so you can't say X[n] matches with Y[n].
If each line contains all of these parameters, then you would be better off using a struct instead of separate vectors:
struct ParameterLine
{
};
QVector< ParameterLine > parameterLines;
// ...
if(temporal==separatorX){
ParameterLine currentLine;
temporal.
insert(0,
QString::number(jj
+1));
currentLine. infoNumero = temporal; // Next change
int c;
if(jj>=0 && jj<9) c = 1;
else if(jj>=9 && jj<99) c = 2;
else if(jj>=99 && jj<999) c = 3;
else c = 4;
temporal = currentLine.infoNumero.mid(0, c);
currentLine.infoNumero = temporal;
// X
temporal = vectorData[ii].mid(posicionX+1, posicionY-1);
currentLine.infoX = temporal; // Next change
// Y
... // etc. for decoding other parameters
parameterLines.push_back( currentLine );
jj++;
}
// And you can retrieve any field using parametersLines[ n ].infoX, etc.
struct ParameterLine
{
QString infoX;
QString infoY;
QString infoNumber;
QString infoD;
};
QVector< ParameterLine > parameterLines;
// ...
if(temporal==separatorX){
ParameterLine currentLine;
temporal.insert(0, QString::number(jj+1));
currentLine. infoNumero = temporal; // Next change
int c;
if(jj>=0 && jj<9) c = 1;
else if(jj>=9 && jj<99) c = 2;
else if(jj>=99 && jj<999) c = 3;
else c = 4;
temporal = currentLine.infoNumero.mid(0, c);
currentLine.infoNumero = temporal;
// X
temporal = vectorData[ii].mid(posicionX+1, posicionY-1);
currentLine.infoX = temporal; // Next change
// Y
... // etc. for decoding other parameters
parameterLines.push_back( currentLine );
jj++;
}
// And you can retrieve any field using parametersLines[ n ].infoX, etc.
To copy to clipboard, switch view to plain text mode
One more trick: instead of the multiple if / else statements to determine the number of digits in jj, try this:
int c= 1 + int( std::log10( float( jj ) ) );
int c= 1 + int( std::log10( float( jj ) ) );
To copy to clipboard, switch view to plain text mode
Bookmarks