trouble using fstream on VC++ 9.0

I have the following code that is not compiling on VC++ 9.0:

//---- strTemp is a std::string which does contain a filename

std::fstream iofs;
iofs.open( (char*)strTemp.c_str(), std::ios::out | std::ios::binary | std::ios::trunc );

It seems the problems are with referencing std::ios::out and the other openmode flags.

Here's the compiler output:

1>c:\Program Files\Microsoft Visual Studio 9.0\VC\include\fstream(934) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\Program Files\Microsoft Visual Studio 9.0\VC\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const std::basic_fstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

Does anyone know what's going on here? Can we not use fstream with VC++ 9.0?

I also have a related question: what are the advantages (if any) of using the complicated iostream classes to handle files, versus the seemingly simpler Windows FILE handle, and functions like fopen(...)?



Don't cast .c_str() to a char*.

You get safer type checking, and it's harder to screw stuff up accidentally (IMO). Btw, a FILE* is C, not Windows.

e.g.:

1
2
3
4
//try to get an unknown line length
fscanf(file, "%s", some_char_ptr); //uh oh, might go out of bounds
//can't use a number because you want the whole line, and you don't know
//how big it is 

v.s.
1
2
3
//try to get an unknown line length
std::getline(file, some_std_string);
//no possible errors unless you are already at the end of file 
Topic archived. No new replies allowed.