別人在讀的文件,我又能鎖住後修改,可行嗎(請看下面這個函數)(100分)

  • 主题发起人 主题发起人 goddy
  • 开始时间 开始时间
G

goddy

Unregistered / Unconfirmed
GUEST, unregistred user!
鎖沒有用SH_DENYWR <br>bool __fastcall TFMain::ReadDBF(String DBFFileName)<br>{<br><br>&nbsp; &nbsp;int handle, status;<br>&nbsp; &nbsp;long length;<br>&nbsp; &nbsp;/* Must have DOS Share.exe loaded for */<br>&nbsp; &nbsp;/* file locking to function properly */<br>&nbsp; &nbsp;//寫INI文件<br>&nbsp; &nbsp; RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":寫Counter.dbf");<br>&nbsp; &nbsp;int i=0;<br><br>&nbsp; &nbsp;//如果打開文件失敗,重試1000次<br>&nbsp; &nbsp;for(i=0;i&lt;1000;i++)<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;//打開dbf文件(共享方式,可讀寫)<br>&nbsp; &nbsp; &nbsp; &nbsp;handle = sopen(DBFFileName.c_str(),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;O_RDWR ,SH_COMPAT ,S_IREAD|S_IWRITE);<br>&nbsp; &nbsp; &nbsp; &nbsp;//打開成功則退出循環<br>// &nbsp; &nbsp; &nbsp; &nbsp;handle = sopen(DBFFileName.c_str(), &nbsp; &nbsp; //別人在讀我打不開了<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; O_RDWR ,SH_DENYWR ,S_IREAD|S_IWRITE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp;if(handle&gt;-1)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; &nbsp;Application-&gt;ProcessMessages();<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; if(handle &lt; 0)<br>&nbsp; &nbsp; {//如果打開1000次文件還不成功,提示打開失敗<br>&nbsp; &nbsp; &nbsp; RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":讀Counter.dbf失敗,有人在使用");<br>&nbsp; &nbsp; &nbsp; ShowMessage("有人在使用,讀No_Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; &nbsp;}<br><br>&nbsp; &nbsp;//如果加鎖文件失敗,重試1000次<br>&nbsp; &nbsp;for(i=0;i&lt;1000;i++)<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp;//從98L開始加鎖10個字節<br>&nbsp; &nbsp; &nbsp;status = lock(handle,98L,10);<br>&nbsp; &nbsp; &nbsp;//如果加鎖成功,退出循環<br>&nbsp; &nbsp; &nbsp;if(status==0)<br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp;Application-&gt;ProcessMessages();<br>&nbsp; &nbsp;}<br><br>&nbsp; &nbsp;if(status!=0)<br>&nbsp; &nbsp;{//如果加鎖1000次都失敗,提示失敗,返回<br>&nbsp; &nbsp; &nbsp;RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":鎖Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;close(handle);<br>&nbsp; &nbsp; &nbsp;ShowMessage("鎖No_Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;return false;<br>&nbsp; &nbsp;}<br><br>&nbsp; &nbsp;//讀舊的文號<br>&nbsp; &nbsp;lseek(handle, 98L, SEEK_SET);<br>&nbsp; &nbsp;char buf[11];<br>&nbsp; &nbsp;if(read(handle, buf, 10)!=10)<br>&nbsp; &nbsp;{//讀取失敗時返回<br>&nbsp; &nbsp; &nbsp;status = unlock(handle,98L,10);<br>&nbsp; &nbsp; &nbsp;close(handle);<br>&nbsp; &nbsp; &nbsp;RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":讀Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;ShowMessage("讀No_Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;return false;<br>&nbsp; &nbsp;}<br><br>&nbsp; &nbsp;//把讀取的文號加1<br>&nbsp; &nbsp;String iTmp=Now().FormatString("YYYY").ToInt()-1911;<br>&nbsp; &nbsp;buf[10]=0;<br>&nbsp; &nbsp;String sTemp=AnsiString(buf);<br>&nbsp; &nbsp;if(sTemp.SubString(1,2).Trim()!=iTmp.Trim())<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iTmp=iTmp+"00000001";<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iTmp=sTemp.ToDouble()+1;<br><br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp;COUNTERNO=iTmp;//把新文件號放在全局變量中<br><br>&nbsp; &nbsp;//把新文號寫回文件<br>&nbsp; &nbsp;lseek(handle, 98L, SEEK_SET);<br>&nbsp; &nbsp;if(write(handle,iTmp.c_str(),10)!=10)<br>&nbsp; &nbsp;{//寫入失敗,解鎖,關閉,返回<br>&nbsp; &nbsp; &nbsp;status = unlock(handle,98L,10);<br>&nbsp; &nbsp; &nbsp;close(handle);<br>&nbsp; &nbsp; &nbsp;RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":寫Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;ShowMessage("寫No_Counter.dbf失敗");<br>&nbsp; &nbsp; &nbsp;return false;<br>&nbsp; &nbsp;}<br><br>&nbsp; &nbsp;//解鎖<br>&nbsp; &nbsp;status = unlock(handle,98L,10);<br>&nbsp; &nbsp;if(status!=0)//解鎖失敗,在INI文件中記錄<br>&nbsp; &nbsp; &nbsp; &nbsp; RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":解鎖Counter.dbf失敗");<br>&nbsp; &nbsp;close(handle);<br>&nbsp; &nbsp;Application-&gt;ProcessMessages();<br>&nbsp; &nbsp;CounterNoTwo=COUNTERNO.SubString(1,2);<br>&nbsp; &nbsp;CounterNoSix=COUNTERNO.SubString(COUNTERNO.Length()-5,6).ToInt(); //去掉前面的0<br><br>&nbsp; &nbsp;RE-&gt;Lines-&gt;Add(DateTimeToStr(Now())+":寫Counter.dbf成功:");<br>&nbsp; &nbsp;return true;<br><br>
 
我現在遇到的問題是:我和別人能同時讀寫No_count.dbf,這個對我們的系統就會出現問題<br>我現在想:我在讀寫時,不會與別人沖突<br>&nbsp; &nbsp;我讀寫時把這個文件鎖住,但別人仍可讀寫<br>以上那個函數不行,大家能否幫我看看問題出在何處,謝謝!
 
你的程序是在什么地方不行的?<br>是什么错?
 
你的程序沒問題啊
 
多人接受答案了。
 
多人接受答案了。
 
后退
顶部