看了 std::ifstream.read() 的源码,问个问题

unhappyless 2008-09-12 05:37:16

// A height for each vertex
std::vector<BYTE> in( _numVertices );

std::ifstream inFile(fileName.c_str(), std::ios_base::binary);

if( inFile == 0 )
return false;

inFile.read(
(char*)&in[0], // buffer
in.size());// number of bytes to read into buffer

inFile.close();


对read() 转定义后到
	_Myt& __CLR_OR_THIS_CALL read(_Elem *_Str, streamsize _Count)
{
return _Read_s(_Str, (size_t)-1, _Count);
}

关键就是 _Read_s 的第二个参数 (size_t)-1 ,它决定了 _Str指针每次的偏移。 typedef unsigned int size_t
std::vector<BYTE> in( _numVertices );而这里定义的内存空间是用 BYTE 。 typedef unsigned char BYTE

int 和 char 所在字节又不等,这里不会溢出吗?
...全文
462 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
unhappyless 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 visame 的回复:]
C/C++ code
basic_istream::_Read_s

Reads a specified number of characters from the stream and stores them in an array.

basic_istream<Elem, Tr>& _Read_s(
char_type *_Str,
size_t _Str_size,
streamsize _Count
);

Parameters

_Str

The array in which to read the characters.
_Str_size

The size of _Str. //显然这里的_Str_size表示_Str的大小
_Count

The nu…
[/Quote]

既然是表示大小,哪也不应该是 (size_t)-1 呀
星羽 2008-09-15
  • 打赏
  • 举报
回复
_Str_size 是目标缓冲的大小,单位是byte
_Count 是需要拷贝(读入)的数据大小

_Str_size 必须大于等于 _Count 否则报错

这个类似于memcpy_s(
void *dest,
size_t numberOfElements,
const void *src,
size_t count
);

里的 numberOfElements 和 count

而_Read_s,在缓冲区有数据的时候,最终会调用 memcpy_s

而 _Str_size 刚好传给 numberOfElements

设成 (size_t)-1
表示不用关心目标缓冲大小(暗示缓冲一定够大以存在读入数据)
星羽 2008-09-15
  • 打赏
  • 举报
回复
设成 (size_t)-1
表示不用关心目标缓冲大小(暗示缓冲一定够大以存在读入数据)
星羽 2008-09-15
  • 打赏
  • 举报
回复
_Str_size 是目标缓冲的代码,单位是byte
_Count 是需要拷贝(读入)的元素个数

_Str_size 必须大于等于 _Count 否则报错

这个类似于memcpy_s(
void *dest,
size_t numberOfElements,
const void *src,
size_t count
);

里的 numberOfElements 和 count

而_Read_s,在缓冲区有数据的时候,最终会调用 memcpy_s

而 _Str_size 刚好传给 numberOfElements
visame 2008-09-14
  • 打赏
  • 举报
回复

basic_istream::_Read_s

Reads a specified number of characters from the stream and stores them in an array.

basic_istream<Elem, Tr>& _Read_s(
char_type *_Str,
size_t _Str_size,
streamsize _Count
);

Parameters

_Str

The array in which to read the characters.
_Str_size

The size of _Str. //显然这里的_Str_size表示_Str的大小
_Count

The number of characters to read.

// basic_istream__Read_s.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main()
{
const int c_size = 10;
char c[c_size];
int count = 5;

cout << "Type 'abcde': ";

cin._Read_s(&c[0], c_size, count);
c[count] = 0;

cout << c << endl;
}

帅得不敢出门 2008-09-14
  • 打赏
  • 举报
回复
貌似 (size_t)-1很大.
unhappyless 2008-09-13
  • 打赏
  • 举报
回复
那读取步长为什么是(size_t)-1 ?
zhuwanglove 2008-09-12
  • 打赏
  • 举报
回复
(size_t-1),读取的步长哦,字节数还是_count,不是(size_t-1)*_count .
不知是否对?
xkyx_cn 2008-09-12
  • 打赏
  • 举报
回复
_Count才是需要读入的字节数

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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