0

ファイルのヘッダーを読み取るためのコードがありcsvます。まず、エクステンソンが存在するかどうかを確認し.csvます。次に、ファイルを読みました。しかし、問題は、他のファイルの名前を変更し、.xmlファイルまたは.docxファイル.csvを読み込もうとすると、このファイル拡張子のチェックが機能しないことです。その後、クラッシュします。
しかし、そのような場合は適切なエラーをスローしたいと考えています。誰でも助けることができますか?関連するコード スニペットは次のとおりです。

    // 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);
4

1 に答える 1

1
while(sChar[lCharIndx] = fgetwc(fpInpFile))

この方法でファイルの終わりをチェックするべきではありません。その代わり:

wint_t wch;
while ((wch = fgetwc(fpInpFile)) != WEOF)
{
    sChar[lCharIndx] = wch;

lCharIndxまた、配列サイズ内にあるかどうかを確認する必要がありますsChar

于 2014-09-11T08:35:05.190 に答える