开发者

getc with Windows vs Unix

开发者 https://www.devze.com 2023-03-18 14:08 出处:网络
I have a question regarding the following code: while((c = getc(pFile)) != EOF) { if(c != \'\\n\') { input[index] = (char)c;

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开发者_Python百科, 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, i.e. 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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消