wjj1991 2016-03-04 17:29 采纳率: 0%
浏览 1982

c++ ifstream read 与 getline 方法获取到的结果不一样

同一个文件,不同方法结果不一样

第1种写法:

  std::ifstream in("midfile.mid");

  in.seekg(0,std::ios::end);
    const int length = in.tellg();
    char* buffer = new char[length];

    in.read(buffer, length);
    in.close();
    std::cout << std::hex << buffer[0];
    delete buffer;

断点显示buffer[0] -51

第2种写法:

std::ifstream in("midfile.mid");

char buffer[256];
while (!in.eof())
{
    in.getline(buffer, 100);
    std::cout << std::hex <<buffer[0];
    break;
}
in.close();

断点显示buffer[0] 77

求问,getline 与 read 有什么不同么?怎么让read到的与getline的结果一样?

  • 写回答

3条回答 默认 最新

  • threenewbee 2016-03-04 22:06
    关注

    const int length = in.tellg();
    返回的不是长度,而是当前文件指针的位置

    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了