getchar在实际EOF之前返回EOF

时间:2016-10-11 14:32:45

标签: c std

问题:在哪种情况下(除了实际到达文件末尾)是C-stdlib返回EOF的getchar函数?

我的目标是将整个文件读入内存缓冲区。所以我想我打开一个文件,检查它的大小,为它分配一个缓冲区,如果有效,那么在for循环中读取文件的每个char到分配的内存中:

int main ( int argc , char *argv[] )
{
    long cpos,epos,l;   
    int i;
    char *p;

    if ( argc == 1)
    {
        printf ("FAIL : no file specified\n");
        exit (-255);
    }
    else
    {
        myMdfFile = fopen ( argv[1] , "r" );
        if (myMdfFile == NULL)
        {
            printf ("FAIL : cannot open file %s\n" , argv[1]);
            exit (-254);            
        }
        else
        {
            fseek( myMdfFile , 0 , SEEK_END );
            epos = ftell(myMdfFile);
            fseek( myMdfFile , 0 , SEEK_SET );  

            pAll = malloc( epos );

            if ( pAll )
            {
                p = (char *)pAll;

                for (l=0;l<epos;l++)
                {
                    i = fgetc(myMdfFile);
                    if ( i == EOF )
                    {
                        if ( l == epos-1 )
                        {
                            printf ("NOTE : %u bytes read \n" , l);
                        }
                        else
                        {
                            printf ("NOTE : file has error errno : %d [0x%02x]\n", errno , errno );
                            cpos = ftell(myMdfFile);
                            printf ("pos is at 0x%08x" , cpos);
                            printf (" ... ...  EOF is at 0x%08x \n" , epos );
                            printf ("Why is the file giving me at %d EOF actually %d bytes before real EOF\n" , l , epos - cpos );
                            exit (-248);                
                        }
                    }   
                    else
                    {
                        *p = i;
                        p++;
                    }
                }
                free (pAll);    
            }
            else
            {
                printf ("FAIL : cannot allocate %u bytes of memory \n" , epos);
                exit (-255);
            }
        }
    }
    return 0;
}

但是getchar并没有让我知道文件中的内容。它自发地决定返回EOF:

D:\rampage\mdfun>mdfun.exe 3064213.mdf
NOTE : file has error errno : 0 [0x00]
pos is at 0x00002000 ... ... EOF is at 0x000b571b
Why is the file giving me at 7136 EOF actually 735003 bytes before real EOF

第7137个字节(l从0开始)是0x00(另见:binary file 3064213.mdf contents at position of l at exit)。 在文件中的位置0x2000也是普通的0x00。 (另见:binary file 3064213.mdf contents at position of cpos at exit)。

顺便说一下为什么getchar调用次数(在l = 7136中)和streampointer位置(cpos = 0x2000 = 8192)之间存在如此显着的偏差? getchar不应仅通过sizeof(char)来增加每个成功检索到的字符的streampointer位置吗?

我在win7 dos shell中使用mingw:

Reading specs from C:/Programme/Razorcat/MSYS_MinGW/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs
Configured with: ../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.5 (mingw-vista special r3)

0 个答案:

没有答案