日期:2014-05-16  浏览次数:20622 次

文件終端显示问 题
程序如 下 :
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
  int p;
  char buf[2048];
  int flag;
  if((p=open("text",O_RDWR))==-1)
  {
  printf("file open error\n!");
  exit(-1);
  }else
  {
  if((flag=read(p,buf,2048))==-1){
  printf("read fail,try again\n");
  exit(-1);
  }
  else
  write(STDOUT_FILENO,buf,2048);
  }
  return 0;
}
cc rw.c
./a.out
显示:? ELF0m4|g4 linux-c$ a.out
(ED444@@???????(?(?1?1?1?'?W|M|M|M??tttDD?1?1?1@P?td??????,3,3Q?tdR?td?1?1?1,???)???'????4??(??6?)??? i|????%?w ?w ?i-laptop:~/linux-c$ ./a.out
|???|???(????0&?B$?+??|????)???????P(????? ??r8$?????|?P$???|??$|?|??E? 4C???)??? |????|$ ?
@)??R0)??4C$)??T
|?$?D???(????)??$)??0)??p$?)???E$?? ??o????)????
?)??&?w ???Ha?<?$?D?)??F?)???B4C? ii
|?|? ??|?|?|????%????P%??A,?
?(*??@)??????4*???? ?E?????l*??????*?,}0?,<*????? |?
GEdP.?)WO_? |?.N=?H
  P1m5? |?|???? ,???P+???
$+??R+??,+??T
H
  |????????B??*??? ??????*????? ???*??9?$????? ??*???D?? +?
请问何解?

------解决方案--------------------
C/C++ code

else
  write(STDOUT_FILENO,buf,2048);

------解决方案--------------------
write(STDOUT_FILENO,buf,2048);

文件里的内容不一定全是可显示的字符,可以用记事本打开看一下
------解决方案--------------------
可能是编码的问题。把ssh客户端的编码改成UTF-8试试。
------解决方案--------------------
貌似是编码问题