Sharing file w/ fstream

Started by
3 comments, last by jesterlecodeur 21 years, 11 months ago
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
_______________
Jester, studient programmerThe Jester Home in French
Advertisement
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!
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
_______________
Jester, studient programmerThe Jester Home in French
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...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This is annoying, thx for the reply all.

_______________

Jester, studient programmer
The Jester Home in French
_______________
Jester, studient programmerThe Jester Home in French

This topic is closed to new replies.

Advertisement