首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

borland c++读文本后,输出文本如何不换行

2012-02-23 
borland c++读文本后,输出文本怎么不换行?#includeconio.h#includestdio.h#includedos.h#includest

borland c++读文本后,输出文本怎么不换行?
#include     <conio.h>    
#include     <stdio.h>    
#include     <dos.h>    
#include     <string.h>    
代码如下:    
 
#include     <stdlib.h>    
#include     <io.h>    
#include     <fcntl.h>    
void     main()    
{    
                      int     fp,n;    
                      const     int     buffsize=1024;    
                      char     buff[buffsize];    
                      fp     =     open( "d:\\1.txt ",     O_RDONLY);    
                      if((n     =     read(fp,     buff,     buffsize))     >     0)        
                      printf(     "%s\n ",     buff);    
                      close(fp);    
}    
我用超级终端传了一个文件1.txt     到另外一台机器上,保存在d:\\1.txt,然后运行上面的程序.    
1.txt的内容如下:    
1hkhjluopjklk4    
111        
 
d:\\1.txt的内容如下:    
1hkhjluopjklk4▊111        
 
程序输出为:    
1hkhjluopjklk4111        
 
这是为什么啊?

[解决办法]
你调试一下程序,在if((n = read(fp, buff, buffsize)) > 0)后边,将buf的内容看一下,如果调试不了的话,那就将buf中的内容转换成16进制打印出来,你看看1hkhjluopjklk4后边的字节是什么,理论上应该是0x0D,如果不是,你就看看源文件的内容吧。如果是,那就有问题了,可能是这个open的方式有错误。你可以用fopen和fread看看。
[解决办法]
\r\n转换问题,用binary模式
[解决办法]
fp = open( "d:\\1.txt ", O_RDONLY);
==》
fp = open( "d:\\1.txt ", O_RDONLY|O_BINARY);

热点排行