Zhang Jiuan’ Notes

解决linux字符串打印错乱问题

碰到这样一件怪事,使用php来处理文件,见如下代码:

$fp = fopen(’aaa’, ‘r’);

while (!feof($fp)) {

    $line = fgets($fp);

    preg_match($pattern, $line, $arr_output);

   //print the some fields of output.

    ……..

}

执行效果完全错乱了,什么原因?

一开始想到的是不是.链接有问题(field1 . fieldn);

于是改成了sprintf,可效果依旧错乱。

打开文件也没有发现什么异常。

经过几番努力,最终发现了症节所在,原来在各个系统上换行不一致\r\n或\r\r\n或\n,

实际这种各个系统间的不一致现象是普遍存在的,所以对文件处理要格外注意。

 

正是因为这些换行不一致引起了打印错乱。

最终对该文件做了一遍换行处理,问题解决。

thx

张久安

If you enjoyed this post, make sure you subscribe to my RSS feed!

No Comments, Comment or Ping

Reply to “解决linux字符串打印错乱问题”

You must be logged in to post a comment.

返回顶部