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

ostream:write函数的类型转换有关问题

2012-03-06 
ostream::write函数的类型转换问题初学C++,对IO函数还不是太了解,因此错误百出啊。。。#include ..\datalog.

ostream::write函数的类型转换问题
初学C++,对IO函数还不是太了解,因此错误百出啊。。。

#include "..\datalog.h"
#include <fstream.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
  ofstream data("data.txt");
  ofstream bindata("data.bin",ios::binary);
  time_t timer = time(NULL);//get time
  //seed random generator:
  srand((unsigned)timer);
  for(int i = 0;i<100;i++)
  {
  datapoint d;
  //convert date/time to a structure:
  d.Time(*localtime(&timer));
  timer += 55;
  d.latitude("45*20'31\"");
  d.longitude("22*34'18\"");
  //zero to 199 meters:
  double newdepth = rand() % 200;
  double fraction = rand() % 100 + 1;
  newdepth += double(1)/fraction;
  d.depth(newdepth);
  double newtemp = 150 + rand()%200;
  fraction = rand() % 100 + 1;
  newtemp += (double)1/fraction;
  d.temperature(newtemp);
  d.print(data);
  bindata.write((unsigned char*)&d,sizeof(d));//这里出问题了
  }
}


||=== 生成测试数据, Debug ===|
\main.cpp|33|error: invalid conversion from `unsigned char*' to `const char*'|
\main.cpp|33|error: initializing argument 1 of `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'|
||=== Build finished: 2 errors, 1 warnings ===|


[解决办法]

探讨
bindata.write((char*)&amp;d,sizeof(d));//write第一个参数的类型应该是char*

热点排行