【发布时间】:2014-11-05 02:54:26
【问题描述】:
我有一个读取csv 文件头的代码。首先我检查扩展是否为.csv。然后我读了文件。但问题是,如果我重命名任何其他文件,例如将.xml 文件或.docx 文件改名为.csv,然后尝试读取它,则此文件扩展名检查不起作用。然后它崩溃了。
但我想在这种情况下抛出一个适当的错误。任何人都可以帮忙吗?以下是相关代码sn-ps:
// Extracting the extension of the file. Since only csv files are allowed,
// for rest of the extensions, appropriate error is being thrown
sExt = wcsrchr(sFile, L'.');
if(wcscmp(sExt, L".csv") != 0)
{
return -1;
}
_wfopen_s(&fpInpFile, sFile, L"rt");
if(fpInpFile == NULL)
{
return -1;
}
while(sChar[lCharIndx] = fgetwc(fpInpFile))
{
lVarLength ++;
lHeaderLength ++;
// If Variable name is too long, or if the length of the header is too long, throw an error
if(lVarLength >= 100 || lHeaderLength >= 100)
{
fclose(fpInpFile);
return -1;
}
// Resetting varibale length before reading length of next variable
if(sChar[lCharIndx] == ',')
lVarLength = 0;
// Header reading is done, so exiting the loop
if(sChar[lCharIndx] == '\n')
break;
lCharIndx ++;
}
fclose(fpInpFile);
【问题讨论】:
标签: c csv file-io error-handling wchar