打开fstream的问题 [英] problem opening fstream

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

问题描述

使用RH9,来自这个片段:


fstream theStream_;

string fPath =" myFile";

int mode = O_EXCL | O_CREAT;

theStream_.open(fPath.c_str(),mode);


我得到的失败如下所示。根据手册页和

标准,第二个参数应该是int。

预先感谢您的帮助。


迈克。


-


g ++ -I。 -I ../ MdbUtil -c -o UniqueFile.o UniqueFile.cc

UniqueFile.cc:在成员函数`void MdbUtil :: UniqueFile :: open(const char *,

const char *,int,int,const char *)'':

UniqueFile.cc:64:从`int''无效转换为`std :: _ Ios_Openmode''
UniqueFile.cc:64:初始化`void std :: basic_fstream< _CharT,

_Traits> :: open(const char *,std :: _ Ios_Openmode)的参数2 [与_CharT = char,_Traits

= std :: char_traits< char>]''

make:*** [UniqueFile.o]错误1

解决方案

" Mike - EMAIL IGNORED" < M _ ************* @ yahoo.com>写了...

使用RH9,来自这个片段:

fstream theStream_;
string fPath =" myFile" ;;
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);


什么是'O_EXCL | O_CREAT''表达''fstream :: open''?

你应该使用''std :: ios_base''中的常量......

我得到如下所示的失败。根据手册页和标准,第二个参数应该是int。


根据什么标准?什么页面/子句/段落?我的

标准副本声明第二个参数的类型为

''std :: ios_base :: openmode''。

谢谢提前帮助。

迈克。

-

g ++ -I。 -I ../ MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc:在成员函数`void MdbUtil :: UniqueFile :: open(const
char *,const char *,int ,int,const char *)'':
UniqueFile.cc:64:从`int''无效转换为`std :: _ Ios_Openmode''
UniqueFile.cc:64:初始化参数2的` void
std :: basic_fstream< _CharT,_Traits> :: open(const char *,std :: _ Ios_Openmode)[with _CharT = char,
_Traits = std :: char_traits< char>]''
make:*** [UniqueFile.o]错误1



是的,我现在看到了我的错误。我的要求是打开一个ofstream,

,如果文件已经存在则让它失败。我查看了std :: ios_base中的

常量,就像旧的fstream功能一样,

它似乎没有语义来执行此操作。如果我对此有正确的判断,那么检查文件存在的最佳方法是什么?

再次感谢您的帮助,

Mike。

Victor Bazarov写道:


Mike - EMAIL IGNORED < M _ ************* @ yahoo.com>写了...

使用RH9,来自这个片段:

fstream theStream_;
string fPath =" myFile" ;;
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);



'O_EXCL是什么| O_CREAT''表达式在''fstream :: open''吗?
你应该使用''std :: ios_base''中的常量......

<我得到了下面显示的失败。根据手册页和标准,第二个参数应该是一个int。



根据什么标准?什么页面/子句/段落?我的
标准副本声明第二个参数的类型为
''std :: ios_base :: openmode''。

提前感谢您的支持麦克。

-I ../ MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc:在成员函数`void MdbUtil :: UniqueFile :: open(const


char *,< blockquote class =post_quotes> const char *,int,int,const char *)'':
UniqueFile.cc:64:从`int''无效转换为`std :: _ Ios_Openmode'' UniqueFile.cc:64:初始化`void

的参数2
std :: basic_fstream< _CharT,

_Traits> :: open(const char *,std :: _ Ios_Openmode )[with _CharT = char,


_Traits

= std :: char_traits< char>]''
make:*** [UniqueFile.o]错误1



" Mike - EMAIL IGNORED" < M _ ************* @ yahoo.com>写道:

Victor Bazarov写道:


Mike - EMAIL IGNORED < M _ ************* @ yahoo.com>写了...

使用RH9,来自这个片段:

fstream theStream_;
string fPath =" myFile" ;;
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);



'O_EXCL是什么| O_CREAT''表达式在''fstream :: open''吗?
你应该使用''std :: ios_base''中的常量......

<我得到了下面显示的失败。根据手册页和标准,第二个参数应该是一个int。



根据什么标准?什么页面/子句/段落?我的
标准副本声明第二个参数的类型为
''std :: ios_base :: openmode''。

提前感谢您的支持麦克。

-I ../ MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc:在成员函数中`void
MdbUtil :: UniqueFile :: open(const

char *,

const char *,int,int,const char *)'':
UniqueFile.cc:64:从`int'转换无效'to


`std :: _ Ios_Openmode''UnityFile.cc:64:初始化`void

的参数2
std :: basic_fstream< _CharT,

_Traits> :: open(const char *,std :: _ Ios_Openmode)[with _CharT =



char,_Traits

= std :: char_traits< char>]''
make:*** [UniqueFile.o]错误1



是的,我看到了我的现在错了。我的要求是打开一个ofstream,
如果文件已经存在则让它失败。我查看了std :: ios_base中的
常量,并且像旧的fstream功能一样,
它似乎没有语义来执行此操作。如果我对此是正确的,检查文件存在的最佳方法是什么?




(a)请不要't top-post


(b)我在C ++中知道检查文件是否存在的唯一方法是打开

文件进行阅读[只有]并检查它是否成功。我的

猜测如果它没有成功(没有这样的文件),那么

你重新打开流写进行并继续使用它(如果我

正确理解你的意图。


Victor


Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);

I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const char*,
const char*, int, int, const char*)'':
UniqueFile.cc:64: invalid conversion from `int'' to `std::_Ios_Openmode''
UniqueFile.cc:64: initializing argument 2 of `void std::basic_fstream<_CharT,
_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits
= std::char_traits<char>]''
make: *** [UniqueFile.o] Error 1

解决方案

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...

Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);
What does ''O_EXCL | O_CREAT'' expression doing in ''fstream::open''?
You are supposed to use constants from ''std::ios_base''...

I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.
According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
''std::ios_base::openmode''.
Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const char*, const char*, int, int, const char*)'':
UniqueFile.cc:64: invalid conversion from `int'' to `std::_Ios_Openmode''
UniqueFile.cc:64: initializing argument 2 of `void std::basic_fstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]''
make: *** [UniqueFile.o] Error 1



Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?
Thanks again for your help,
Mike.
Victor Bazarov wrote:


"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...

Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);



What does ''O_EXCL | O_CREAT'' expression doing in ''fstream::open''?
You are supposed to use constants from ''std::ios_base''...


I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.



According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
''std::ios_base::openmode''.

Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const


char*,

const char*, int, int, const char*)'':
UniqueFile.cc:64: invalid conversion from `int'' to `std::_Ios_Openmode''
UniqueFile.cc:64: initializing argument 2 of `void


std::basic_fstream<_CharT,

_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char,


_Traits

= std::char_traits<char>]''
make: *** [UniqueFile.o] Error 1



"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...

Victor Bazarov wrote:


"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...

Using RH9, from this fragment:

fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);



What does ''O_EXCL | O_CREAT'' expression doing in ''fstream::open''?
You are supposed to use constants from ''std::ios_base''...


I get the failure shown below. According to the man pages and
the standard, the second argument should be an int.



According to what standard? What page/clause/paragraph? My copy of the
standard states that the second argument has the type
''std::ios_base::openmode''.

Thanks in advance for your help.

Mike.

--

g++ -I. -I../MdbUtil -c -o UniqueFile.o UniqueFile.cc
UniqueFile.cc: In member function `void MdbUtil::UniqueFile::open(const

char*,

const char*, int, int, const char*)'':
UniqueFile.cc:64: invalid conversion from `int'' to

`std::_Ios_Openmode'' UniqueFile.cc:64: initializing argument 2 of `void


std::basic_fstream<_CharT,

_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT =


char, _Traits

= std::char_traits<char>]''
make: *** [UniqueFile.o] Error 1



Yes, I see my error now. My requirement is to open an ofstream,
and have it fail if the file already exists. I looked at the
constants in std::ios_base, and like the old fstream capability,
it does not appear to have the semantics to do this. If I am
correct about this, what is the best way to check for the
existence of a file?



(a) Please don''t top-post

(b) The only way I know in C++ to check if a file exists is to open
the file for reading [only] and check whether it succeeds. My
guess is that if it doesn''t succeed (there is no such file), then
you re-open the stream for writing and continue using it (if I
understood your intentions correctly).

Victor


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

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