| Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате |
| Форум программистов > C++ Builder > Windows XP File Overwrite |
| Автор: StrikeTheCore 4.8.2006, 20:20 |
| Hi, Question for any of you English-speaking Borland experts out there. I have a program in Borland 6 running on Windows XP that needs to be able to save a file (overwriting the previous file) using std::ofstream. However the file is on a network and is often open on other computers which prevents the computer that needs to overwrite it from having write access. Is there any way to check to see if this file is already open so the user can be warned that any changes made will not be saved? Thanks -Brian |
| Автор: SergeCpp 12.8.2006, 22:58 |
| Hi, Brian! Try to open that file through API's http://msdn.microsoft.com/library/en-us/fileio/fs/createfile.asp with dwDesiredAccess = GENERIC_WRITE and dwShareMode = 0 Then close it and create ofstream. But you are risking... File can be reopened remotely in between... So create ofstream as quickly as possible... It was quick-and-dirty method, but it's simple... Serge |
| Автор: stmamont 13.8.2006, 08:27 |
| Is there any method to lock file quickly? And only then write to file? |
| Автор: SergeCpp 13.8.2006, 17:52 |
| You can hold file opened for writing with exclusive access. Or you can use http://msdn.microsoft.com/library/en-us/fileio/fs/lockfile.asp or http://msdn.microsoft.com/library/en-us/fileio/fs/lockfileex.asp API. |
| Автор: stmamont 14.8.2006, 22:34 |
| But, as I understood LockFile - is only used by WriteFile, ReadFile but not by std::ofstream |