簡體   English   中英

當 stream 不為 0 時,fgetc() 返回 EOF

[英]fgetc() returns EOF when stream is not 0

我正在嘗試從二進制文件中讀取字符並將它們與另一個二進制文件中的字符進行比較,但我想從中間開始讀取其中一個文件,而不是從頭開始。 當我嘗試它時,我無法從中間讀取我試圖讀取的文件,因為它立即返回 EOF。 我嘗試從不同的角度讀取它,甚至將 stream 移動到一個位置,但它仍然立即返回 EOF。

int scanWithOffset(FILE* fc, FILE* fv, int start, int end)
{
    int charRead1 = 0, charRead2 = 0, matchCounter = 0, match = 0;
    int fileSize = findFileSize(fv);
    int counter = 0;

    if (!fseek(fc, start, SEEK_SET))
    {
        while (((charRead2 = fgetc(fv)) != EOF) && ((charRead1 = fgetc(fc)) != EOF)
               && !match && counter < (end - start))
        {
            counter++;
            if (charRead1 == charRead2)
            {
                matchCounter++;
                if (matchCounter == fileSize)
                {
                    match = 1;
                    fclose(fc);
                }
            }
            else
            {
                // if something doesn't match up, reset the counter and bring
                // the stream back to the beginning
                matchCounter = 0; 
                fseek(fv, 0, SEEK_SET);
            }
        }

        if (!match)
        {
            fclose(fc);
        }
    }

    return match;
}
if (matchCounter == fileSize)
{
   match = 1;
   fclose(fc);
}

關閉fc后不要中斷while循環。 當您到達matchCounter == fileSize時,您必須退出while循環。 您可以添加break; fclose(fc)之后:

if (matchCounter == fileSize)
{
   match = 1;
   fclose(fc);
   break;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM