在我的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 METHOD
/** 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;
}
答案 0 :(得分:3)
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个以上的元素。见erase documentation:
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()
....