二进制标准输入输出防止"\r\n"与"\n"之间自重转换
2013-12-31 19:02
344 查看
使用 std::cin.read() 从文件或者管道中读取字符时,当读到 "\r\n" 时会自动转换为 "\n",同样std::cout.write() 写"\n" 时会写成 "\r\n"。
如果以二进制模式读写文件则不会有该问题,可以采用:
如果以二进制模式读写文件则不会有该问题,可以采用:
std::filebuf fb; fb.open ("test2.txt", std::ios::in | std::ios::binary);一种更通用的方式是使用 ReadFile 和 WriteFile
HANDLE std_input_handle = ::GetStdHandle(STD_INPUT_HANDLE); while (size > 0) { DWORD bytes_read = 0; if (::ReadFile(std_input_handle, buff, size, &bytes_read, NULL)) { size -= bytes_read; buff += bytes_read; } else { ret = false; break; } }
HANDLE std_output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); ::WriteFile(std_output_handle, buff, size, &write_size, NULL);
相关文章推荐
- “yyyymmddhhmiss"、tm、time_t、ASN1_TIME 之间的类型转换
- 经纬度"度分秒"与"十进制"之间的转换
- 左值引用"&"、右值引用"&&"以及它们之间的转换
- 经纬度"度分秒"与"十进制"之间的转换
- 二进制、八进制、十进制、十六进制之间转换 <转>
- SQL Server 汉语日期和"标准日期"的转换
- 用标准C++进行string与各种内置类型数据之间的转换
- DataSet和List<T> 泛型之间互相转换
- 字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding .
- 十进制与二进制、八进制、十六进制等之间的转换
- 二进制与对象之间的转换
- php接收二进制文件转换成图片,php://input $GLOBALS['HTTP_RAW_POST_DATA']
- Android图片二进制与Bitmap、Drawable之间的转换
- 二进制与格雷码之间的转换---矩阵表示
- 时间戳标准日期之间的转换
- C语言关于"0" '0' '\0'之间的区别。
- "ICS"与"NAT"转换有什么区别?
- java中""," ",null之间的区别
- cvLoadImage 不能将参数1从 "_TCHAR*" 转换为"const char *"
- 小朋友学C语言(23):二进制与十进制之间的转换