ifstream的 [英] ifstream

查看:90
本文介绍了ifstream的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ifstream ifs;

ifs.open(&whatever.bin",ios :: in | ios :: binary);


如何设置上面的标志,以便如果whatever.bin文件丢失,而不是

创建一个名为whatever.bin的新文件,但是提供了一种检测

错误的方法吗?


ifs.read((char *)p,4);


每个都是read()以上调用只读取字节数?

是否可以直接读取整数?比如ifs.read((int *)p,1);


ifs.read((char *)p,16);


如何从p中获得指向16个字节的4个整数?


谢谢!

解决方案

" ;达特" <哒****** @ dickto.com>写道...

ifstream ifs;
ifs.open(" whatever.bin",ios :: in | ios :: binary);

如何设置上面的标志,以便如果whatever.bin文件丢失,而不是创建一个名为whatever.bin的新文件,但提供了一种检测
错误的方法?


因为你使用''ios :: in'',它不应该创建。为什么会这样?


标准说''open''可以设置''failbit'',所以在尝试打开文件后打开
,检查它是否确实打开:


if(ifs.is_open())

...

ifs。 read((char *)p,4);

每个都是read()以上调用只读取字节数?是否有可能直接读取整数?像ifs.read((int *)p,1);


不,低级I / O仅适用于字符。

ifs.read((char *)p,16);

如何从p中获得指向16个字节的4个整数?




通常你可以简单地重新解释_cast指针来对待你的''p''

作为一组int。它不是便携式的,当然,使用你自己的风险。


Victor


< blockquote> Dart写道:

ifstream ifs;
ifs.open(" whatever.bin",ios :: in | ios :: binary);


如果(数据文件的)可移植性是一个问题,你应该考虑

做格式化的输入和输出(用<<>> ;)。

如何设置上面的标志,以便如果whatever.bin文件丢失,而不是创建一个名为whatever.bin的新文件,但提供了一种检测
错误的方法?


在我的系统中,行为已经是你要求的了。我不能证明这个可移植性,因为标准只是说

文件是打开的好像通过调用fopen,我的fopen

的文档在这一点上并不清楚。

ifs.read((char *)p,4);

每个read()是否都是以上调用只读取字节数?


是。

是否可以直接读取整数?像ifs.read((int *)p,1);


这取决于你的意思。这是一个想法:


模板< typename T>

std :: istream& read_natively(std :: istream& stream,T& var)

{

char * mem = reinterpret_cast< char *> (& var);

返回stream.read(mem,sizeof var);

}


// .. 。


int x;

if(read_natively(ifs,x))/ * ... * /;

ifs .read((char *)p,16);

如何从p中获得指向16个字节的4个整数?




int a [4];

read_natively(ifs,a); //可能无法在破旧的编译器上工作


-

问候,

巴斯特。

Victor Bazarov< v。******** @ comAcast.net>在消息新闻中写道:du2fc.32356>

ifs.read((char *)p,16);


如何从p获得4个整数指向16个字节?



通常你可以简单地重新解释指针,把你的''p''作为一个整数数组来对待。它不是便携式的,当然,使用你自己的风险。

Victor



这会起作用吗?


int arr [4];

arr = reinterpret_cast< int *>(p);


谢谢!


ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);

How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?

ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes? Is it possible to
directly read in ints? Like ifs.read((int *)p, 1);

ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?

Thanks!

解决方案

"Dart" <da******@dickto.com> wrote...

ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);

How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?
Since you use ''ios::in'', it shouldn''t create. Why would it?

The Standard says that ''open'' can set ''failbit'', so after an attempt
to open a file, check if it has been in fact open:

if (ifs.is_open())
...

ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes? Is it possible to
directly read in ints? Like ifs.read((int *)p, 1);
No, the low-level I/O works only in chars.

ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?



Often you can simply reinterpret_cast the pointer to treat your ''p''
as an array of ints. It''s not portable, of course, use at your own
risk.

Victor


Dart wrote:

ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);
If portability (of the data files) is a concern you should consider
doing formatted input and output instead (with << and >>).
How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?
On my system the behaviour is already what you''re asking for. I can''t
attest to the portability of this, as the standard just says that the
file is opened "as if" by calling fopen, and my documentation for fopen
isn''t clear on this point.
ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes?
Yes.
Is it possible to directly read in ints? Like ifs.read((int *)p, 1);
It depends what you mean by directly. Here''s an idea:

template <typename T>
std::istream & read_natively (std::istream & stream, T & var)
{
char * mem = reinterpret_cast <char *> (& var);
return stream.read (mem, sizeof var);
}

// ...

int x;
if (read_natively (ifs, x)) /* ... */;
ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?



int a [4];
read_natively (ifs, a); // might not work on broken old compilers

--
Regards,
Buster.


Victor Bazarov <v.********@comAcast.net> wrote in message news:du2fc.32356>

ifs.read((char *)p, 16);


How to obtain 4 ints out of p pointing to 16 bytes?



Often you can simply reinterpret_cast the pointer to treat your ''p''
as an array of ints. It''s not portable, of course, use at your own
risk.

Victor


Is this going to work?

int arr[4];
arr = reinterpret_cast<int *>(p);

Thanks!


这篇关于ifstream的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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