[fgetc()当流不为0时返回EOF

问题描述 投票:0回答:1

我正在尝试从二进制文件中读取字符,并将其与另一个二进制文件中的字符进行比较,但是我想从中间开始而不是从头开始读取其中一个文件。尝试时,我无法从中间读取要读取的文件,因为它会立即返回EOF。我试图从不同的角度阅读它,甚至将流移动到一个地方,但它仍然立即返回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;
}
c fseek fgetc
1个回答
0
投票
if (matchCounter == fileSize)
{
   match = 1;
   fclose(fc);
}

关闭while后,您不会中断fc循环。到达while时必须退出matchCounter == fileSize循环。您可以在break;]之后添加fclose(fc)

© www.soinside.com 2019 - 2024. All rights reserved.