fstream不指定std::ios::in和std::ios::out也能读写???

Never_Satisfied 2013-03-21 10:32:55
不指定std::ios::in和std::ios::out也能读写???

std::fstream LoadFile;
LoadFile.open ( "C:\\3V.txt", std::ios::binary | std::ios::_Nocreate );

read可以正常读取数据,何解?
...全文
1633 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Never_Satisfied 2013-03-24
  • 打赏
  • 举报
回复
谢谢
引用 15 楼 tofu_ 的回复:
引用 14 楼 Never_Satisfied 的回复:引用 8 楼 tofu_ 的回复:5#没有回答到点上。 这种问题,必须拿出源代码才能让人信服。 C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535……
Never_Satisfied 2013-03-23
  • 打赏
  • 举报
回复
引用 5 楼 dy106 的回复:
std::fstream LoadFile对象默认打开方式就是读入和输出
孩子,我这不是指定了吗,默认值还会自动 或 上来?
tofu_ 2013-03-23
  • 打赏
  • 举报
回复
引用 14 楼 Never_Satisfied 的回复:
引用 8 楼 tofu_ 的回复:5#没有回答到点上。 这种问题,必须拿出源代码才能让人信服。 C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657template <typename Ch……
在vs下,在fs.open("", XX)这里下断点,然后运行到这儿的时候,选择逐语句,每次遇到函数调用的时候也都F11运行,一层一层进入,最后就会进到源码那里。
_sunshine 2013-03-23
  • 打赏
  • 举报
回复
std::fstream LoadFile对象默认打开方式就是读入和输出
Never_Satisfied 2013-03-23
  • 打赏
  • 举报
回复
引用 8 楼 tofu_ 的回复:
5#没有回答到点上。 这种问题,必须拿出源代码才能让人信服。 C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657template <typename CharT> FILE * _Xfio……
谢谢你的帮助。 请问这些源代码我应该在哪里获得?
Never_Satisfied 2013-03-23
  • 打赏
  • 举报
回复
我去,寡人的系统是定制版的吗?这情况没人遇到过??
mujiok2003 2013-03-23
  • 打赏
  • 举报
回复
引用 12 楼 mujiok2003 的回复:
出了open,别忘了构造函数
除了
mujiok2003 2013-03-23
  • 打赏
  • 举报
回复
出了open,别忘了构造函数
  • 打赏
  • 举报
回复
初始化的时候,编译器会用默认的方式打开文件
_sunshine 2013-03-23
  • 打赏
  • 举报
回复
引用 8 楼 tofu_ 的回复:
5#没有回答到点上。 这种问题,必须拿出源代码才能让人信服。 C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657template <typename CharT> FILE * _Xfio……
学习了
tofu_ 2013-03-23
  • 打赏
  • 举报
回复
上面写错了个小地方,应该是加上ios_base::in,虽然ios::in就是前者。 fstream如果指定了open_mode,默认参数显然不会被传入。如果没有ios_base::_Nocreate标志,只是个ios_base::binary的话,这个fstream是不能用来输入输出的。打开模式与流本身没有任何关系,不存在fstream默认就可以读入和写出的说法。不信可以测试:

fstream fs;
fs.open("test.txt", ios_base::binary);
// fstream.open有一个默认参数放在最后,就是ios_base::_Nocreate
// 所以上面的代码与下面这行功能一样
// fs.open("test.txt", ios_base::binary, ios_base::_Nocreate);
if (fs)
{
    cout << "yes" << endl;  // 这个yes不会输出
}
tofu_ 2013-03-23
  • 打赏
  • 举报
回复
5#没有回答到点上。 这种问题,必须拿出源代码才能让人信服。

template <typename CharT> FILE * _Xfiopen(const CharT *filename,
	ios_base::openmode mode, int prot)
	{
	static const int valid[] =
		{	// valid combinations of open flags
		ios_base::in,
		ios_base::out,
		ios_base::out | ios_base::trunc,
		ios_base::out | ios_base::app,
		ios_base::in | ios_base::binary,
		ios_base::out | ios_base::binary,
		ios_base::out | ios_base::trunc | ios_base::binary,
		ios_base::out | ios_base::app | ios_base::binary,
		ios_base::in | ios_base::out,
		ios_base::in | ios_base::out | ios_base::trunc,
		ios_base::in | ios_base::out | ios_base::app,
		ios_base::in | ios_base::out | ios_base::binary,
		ios_base::in | ios_base::out | ios_base::trunc
			| ios_base::binary,
		ios_base::in | ios_base::out | ios_base::app
			| ios_base::binary,
		0};

	FILE *fp = 0;
	int n;
	ios_base::openmode atendflag = mode & ios_base::ate;
	ios_base::openmode norepflag = mode & ios_base::_Noreplace;

        // ===========重点看这里===========
	if (mode & ios_base::_Nocreate)
		mode |= ios_base::in;	// file must exist
	if (mode & ios_base::app)
		mode |= ios_base::out;	// extension -- app implies out

	mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);
	for (n = 0; valid[n] != 0 && valid[n] != mode; ++n)
		;	// look for a valid mode

	if (valid[n] == 0)
		return (0);	// no valid mode
	else if (norepflag && (mode & (ios_base::out | ios_base::app))
		&& (fp = _Xfsopen(filename, 0, prot)) != 0)
		{	// file must not exist, close and fail
		fclose(fp);
		return (0);
		}
	else if (fp != 0 && fclose(fp) != 0)
		return (0);	// can't close after test open
	else if ((fp = _Xfsopen(filename, n, prot)) == 0)
		return (0);	// open failed

	if (!atendflag || fseek(fp, 0, SEEK_END) == 0)
		return (fp);	// no need to seek to end, or seek succeeded

	fclose(fp);	// can't position at end
	return (0);
	}
上面的函数被fstream的open函数间接调用。可以看到,当第二个参数包含ios_base::_Nocreate标志时,会被自动加上ios::in。楼主不必惊奇。
_sunshine 2013-03-23
  • 打赏
  • 举报
回复
引用 6 楼 Never_Satisfied 的回复:
引用 5 楼 dy106 的回复:std::fstream LoadFile对象默认打开方式就是读入和输出 孩子,我这不是指定了吗,默认值还会自动 或 上来?
对象是对象,函数是函数,二者并不冲突
Never_Satisfied 2013-03-22
  • 打赏
  • 举报
回复
楼上二位一个复制,一个表情,这是不相信我???截个图,以示清白!
love_yourlife 2013-03-21
  • 打赏
  • 举报
回复
wenhong609 2013-03-21
  • 打赏
  • 举报
回复
ios::binary Opens the file in binary mode (the default is text mode). Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files. 确定?!

64,648

社区成员

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

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