多余的CR符号哪来的

fancystyle 2009-09-24 11:44:53
我的操作:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{

FILE* fplog = fopen("test.txt", "a+");
char * str="test\r\n";
fwrite(str,1,strlen(str),fplog);
fclose(fplog);
return 0;
}


打开文件后,内容是
test\r\r\n
我不要多余的这个\r,这多余的玩意哪来的?我不要那玩意如何做.
...全文
185 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fancystyle 2009-09-24
  • 打赏
  • 举报
回复
感谢大家的帮助.问题已解决,用二进制方式打开就行了
看来我的搜索能力不是不行,搜了半天没搜到.
kouwenlong 2009-09-24
  • 打赏
  • 举报
回复
你的文件内容怎么能是test\r\n?他们都是不可打印的字符啊?
可以这样
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{

FILE* fplog = fopen("test.txt", "a+");
char * str="test\\r\\n";
fwrite(str,1,strlen(str),fplog);
fclose(fplog);
return 0;
}

这样就可以见了,而且保持原来的字符。
brookmill 2009-09-24
  • 打赏
  • 举报
回复
可以看看产生的文件是6个字节还是7个
如果用文本方式打开文件,如一楼所说会在写入的时候把\n自动写成\r\n
如果用二进制方式打开文件就不会了。
kouwenlong 2009-09-24
  • 打赏
  • 举报
回复
我打开的文件只有test
brookmill 2009-09-24
  • 打赏
  • 举报
回复
试试fopen("test.txt", "ab+");
liao05050075 2009-09-24
  • 打赏
  • 举报
回复
windows的回车符是\r\n,于是,你向文件写入\n时会自动变成\r\n
但是,当你从文件读入时,\r\n又会自动变成\n了。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧