在我的C++应用程序中,我必须选择一个带有QFileDialog类的文件。然后,我验证我的文件名是否正确(它必须以"VG"开头)。
我的文件有这个结构:VGx-xx-xxxx-xxx-xxx.pigs
之后,我在关联的QLineEdit中设置了它。但是每次我选择一个好的文件时,它都会崩溃,我不明白为什么。
这是我的不同功能:
打开 QFILE 对话框窗口
/** OPEN FILE DIALOG WINDOW **/
void VGCCC::selectPIGSFile()
{
QString pigsFile = QFileDialog::getOpenFileName
(
this,
tr("Select PIGS file"),
"./../VGCColorConfigurator/Inputs",
tr("Document files (*.pigs *.igs)"),
0,
QFileDialog::DontUseNativeDialog
);
pigsPath = pigsFile;
if(verifyPIGSFileValidity(pigsPath.toStdString()))
{
m_filePathLine->setText("");
m_filePathLine->setText(pigsPath);
m_testTextEdit->insertPlainText("File selected : "+pigsPath+"n");
}
else
{
m_filePathLine->setText("Please select a valid PIGS (Format VGx-xx-xxxx-xxx-xxx.pigs)");
m_testTextEdit->insertPlainText("Uncorrect PIGS file.n");
}
}
验证文件名
/** VERIFY SELECTED PIG FILE **/
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
splitPIGSName(pigsPath);
std::string verification = pigsNameTable[0].erase(2,2);
std::string headerName = "VG";
if(!verification.compare(headerName))
{
m_testTextEdit->insertPlainText("PIGS name is correct");
return true;
}
else
return false;
}
拆分方法
/** SPLIT PIGS NAME INTO TABLE **/
std::string* VGCCC::splitPIGSName(std::string pigsPath)
{
std::string pigsPathToSplit = pigsPath;
std::string delimiter = "-";
size_t position = 0;
int i=0;
std::string token;
while ((position = pigsPathToSplit.find(delimiter)) != std::string::npos)
{
token = pigsPathToSplit.substr(0, position);
std::cout << token << std::endl;
pigsNameTable[i] = token;
i++;
pigsPathToSplit.erase(0, position + delimiter.length());
}
pigsNameTable[4] = pigsPathToSplit.c_str();
std::cout << pigsPathToSplit << std::endl;
}
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
splitPIGSName(pigsPath);
std::string verification = pigsNameTable[0].erase(2,2);
std::string headerName = "VG";
if(!verification.compare(headerName))
{
m_testTextEdit->insertPlainText("PIGS name is correct");
return true;
}
else
return false;
}
不安全,因为:
1-您不检查pigsNameTable
索引(如果是vector
?)或键(如果是map
?)是否有元素 0
2-您不检查pigsNameTable[0]
是否有2个以上的元素。请参阅擦除文档:
pos:要擦除的第一个字符的位置。如果这更大 比字符串长度,它抛出out_of_range。
您可以简单地执行以下操作:
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
splitPIGSName(pigsPath);
if ( /* test is pigsNameTable[0] exists depending on pigsNameTable's type */ )
{
return pigsNameTable[0].find( "VG" ) == 0; // return true if pigsNameTable[0] starts with "VG"
}
else
{
return false;
}
}
如果pigsNameTable
是vector
则可以!pigsNameTable.empty()
测试,如果是map
,pigsNameTable.find(0) != pigsNameTable.end()
....