ofstream将无符号的短写到文件 [英] ofstream write unsigned short to file

查看:146
本文介绍了ofstream将无符号的短写到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
std::ofstream fout("..\\INVNTRY.dat"); // output, normal file
unsigned short nianq = 2570;
fout.write ((const char*)&nianq,sizeof(nianq));
fout.close ();
return 0;
}



文件长度为4,但不是2.为什么?
我使用vs.net 2005



the file longth is 4,but not 2.why?
i use vs.net 2005

推荐答案

更改为std :: ofstream fout(".. \\ INVENTRY.dat",ios_base :: binary);

ofstream默认情况下是基于字符的.运行时将插入回车符/换行符.
Change to std::ofstream fout("..\\INVENTRY.dat", ios_base::binary);

ofstream is character-based by default. The runtime inserts a carriage return/line feed.


这篇关于ofstream将无符号的短写到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆