简体   繁体   中英

getc with Windows vs Unix

I have a question regarding the following code:

while((c = getc(pFile)) != EOF)
{
    if(c != '\n')
    {
         input[index] = (char)c;
         index++;
     } else
     {
         input[index] = '\0';
         index = 0;
     }
}

In Windows, this c = getc line reads '\n' (code 10) twice. For example, I'm reading in the file with the following 2 lines:

Hello world
Test

c = getc reads in Hello world, but reads in 10 (\n) and 10 once more, resetting the input array to blank (because of the '\0'). In unix, the '\n' only gets read once, so it all works.

Any idea?

Thanks in advance.

Is the file physically the same, ie bit-by-bit, on the two platforms? That's asking for trouble, since the encoding for line ending differs.

Windows terminate lines with \r\n . May be this could help:

$ echo test | unix2dos > /tmp/test
$ hexdump -c /tmp/test
0000000   t   e   s   t  \r  \n                                        
0000006

Stangely \r value is 13, so I dont know wath is going wrong.

try this:

while((c = getc(pFile)) != EOF)
{
    if(c != '\n' && index)
    {
         input[index] = (char)c;
         index++;
    } 
    else
    {
         if (!index)
              continue; // dumps repeated '\n'

         input[index] = '\0';
         index = 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM