Archived

This topic is now archived and is closed to further replies.

jesterlecodeur

Sharing file w/ fstream

Recommended Posts

jesterlecodeur    122
Is there a way to define the sharing mode of a file w/ fstream? If I make fstream f("toto.txt"); I can''t open the file w/ another process before I close f. Is there a way w/ Standart C++ (I now that the fstream MS specific can do this, but I want make it protable). _______________
Jester, studient programmer The Jester Home in French

Share this post


Link to post
Share on other sites
Oluseyi    2116
The fstream constructor takes three parameters, but two of them have default values. To specify sharing mode, simply do the following:

fstream f ( "filename.ext", ios::in|ios::out, filebuf::sh_read|filebuf::sh_write );


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
jesterlecodeur    122
The problem is:
On VC++:
sh_read and sh_write are defined on fstream.h wich seems to be Microsoft specific (not in the std namespace) and independant of fstream.
The more near in the fstream header:
explicit basic_fstream(const char *_S, ios_base::openmode _M = in | out)

On Digital UNIX V4.0D w/ gcc 2.95:
No sh_read nor sh_write in the fstream.h.
The more affialte ctor i find is :
fstream(const char *name, int mode, int prot=0664);

Maybe that I miss some header?

_______________

Jester, studient programmer
The Jester Home in French

Share this post


Link to post
Share on other sites
Fruny    1658
The concept of file protection, file sharing, file permissions is OS-specific, which is why it is not covered by standard C++

You could always write your own file buffer class, specialised in non-locking access to files and create fstream objects which use your file buffer class.

I understand that''s not what you want to hear, but that''s the only way to do it that I can see... beside using fstream.h

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites