andy7200
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:6 回覆:6 積分:2 註冊:2004-09-27
發送簡訊給我
|
原則上,程式應該是每次Run都有同樣的結果不是嗎? 但是如下的一個小程式卻不然,有時compile完後執行,ediOut1和ediOut2都會出現"file open success",但有時同樣的程序下ediOut1和ediOut2就都顯示"file open failure". 巧的是,從來沒有一個出現"file open success"並且一個出現"file open failure"過.不知到出了什麼問題.煩請各位高手幫我看看好嗎? ps: 1.txt 和 2.txt 內只是幾組數字而已! //--------------------------------------------------------------------------- #include
#pragma hdrstop
#include
#include "Unit1.h"
//-------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------------------
ifstream fin;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
fin.open("c:\\1.txt",ios::in);
if (!fin)
ediOut1->Text="file open failure!";
else
ediOut1->Text="file open success!"; Timer1->Enabled=true;
}
//-------------------------------------------------------------------
int uv=0; char a;
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
uv=uv 1;
fin>>a;
ediOut3->Text=a; if (uv==5)
{
fin.close();
fin.open("c:\\2.txt",ios::in);
if (!fin)
ediOut2->Text="file open failure!";
else
ediOut2->Text="file open success!";
} if (uv==15)
{
fin.close();
Timer1->Enabled=false;
}
}
//-------------------------------------------------------------------
|
Zard
尊榮會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
老實說你的程式怪怪的, 不建議把open file 和close file放在不同的function
, 這樣不但容易出錯, 程式也不好維護吧! , 還有就是建議你把全域變數放
在一起, 程式比較容易維護和閱讀. 你這個程式有幾個問題:
>>Text="file open failure!">Text="file open success!"; Timer1->Enabled=true;
} 2. 你的timer的interval值是多少 ?? 有沒有可能interval值太短在
Timer1Time() 還沒跑完又觸發第二次? 別忘了事件觸發的function
可不會等你跑完才會發生, 所以我的習慣是在timer一開始把timer
關閉, 做完事後在把timer打開, 這樣可以減少出錯的機會, 所以你
的timer可以修改為 void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = FALSE;
uv=uv 1;
fin>>a;
ediOut3->Text=a; if (uv==5)
{
fin.close();
fin.open("c:\\2.txt",ios::in);
if (!fin)
ediOut2->Text="file open failure!";
else
ediOut2->Text="file open success!";
} if (uv==15)
{
fin.close();
Timer1->Enabled=false;
return;
} Timer1->Enabled = TRUE;
}
|
andy7200
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:6 回覆:6 積分:2 註冊:2004-09-27
發送簡訊給我
|
感謝Zard 的幫忙. 但結果還是一樣...,是否還有其它方法嗎? 我的修改如下: //-------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include "Unit1.h"
//-------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------------------
ifstream fin;
int uv=0; char a;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
uv=0;
Timer1->Interval=1000;
fin.open("c:\\2.txt",ios::in);
if (!fin)
ediOut1->Text="file open failure!";
else
ediOut1->Text="file open success!"; Timer1->Enabled=true;
}
//------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled=false;
uv=uv 1;
fin>>a;
ediOut3->Text=a; if (uv==5)
{
fin.close();
fin.open("c:\\1.txt",ios::in);
if (!fin)
ediOut2->Text="file open failure!";
else
ediOut2->Text="file open success!";
} if (uv==10)
{
fin.close();
ediOut3->Text="close";
return;
}
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
|
Zard
尊榮會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
怪了, 我怎麼試不出你說的問題呢? 你的程式碼全都有貼上來嗎? 還是這只是部份的碼呢? 我修改了一下, 在貼一次 c:\1.txt 內容為 12345, c:\2.txt 內容為 abcde 同時我把ediOut3改為Memo, 把所有結果印出.
//-------------------------------------------------------------------
#include
#pragma hdrstop
#include #include "Unit1.h"
//-------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------------------
ifstream fin;
int uv=0; char a;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
uv=0;
Timer1->Interval=100;
Memo1->Lines->Clear();
fin.open("c:\\2.txt",ios::in);
if (!fin)
{
ediOut1->Text="file open failure!";
return;
}
else
ediOut1->Text="file open success!"; Button1->Enabled = FALSE;
Timer1->Enabled=true;
}
//------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled=false;
uv=uv 1;
fin>>a;
Memo1->Lines->Add(a); if (uv==5)
{
fin.close();
fin.open("c:\\1.txt",ios::in);
if (!fin)
{
ediOut2->Text="file open failure!";
return;
}
else
ediOut2->Text="file open success!";
} if (uv==10)
{
fin.close();
Memo1->Lines->Add("close");
Button1->Enabled = TRUE;
return;
}
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
|
blk5743
高階會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:34 回覆:371 積分:236 註冊:2003-11-17
發送簡訊給我
|
參考一下,我剛試過了,是可行的
#include
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
FILE *fp,*fp2;
int uv = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if ( (fp = fopen("1.txt","r")) == NULL )
Edit1->Text = "Fail";
else
Edit1->Text = "OK";
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
uv ;
if ( uv == 5 )
{
if ( fp )
fclose(fp);
if ( (fp2 = fopen("2.txt","r")) == NULL )
Edit2->Text = "Fail";
else
Edit2->Text = "OK";
}
if ( uv == 15 )
{
if ( fp2)
fclose(fp2);
Timer1->Enabled = false;
}
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
|
blk5743
高階會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:34 回覆:371 積分:236 註冊:2003-11-17
發送簡訊給我
|
參考一下,我剛試過了,是可行的
#include
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
FILE *fp,*fp2;
int uv = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if ( (fp = fopen("1.txt","r")) == NULL )
Edit1->Text = "Fail";
else
Edit1->Text = "OK";
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
uv ;
if ( uv == 5 )
{
if ( fp )
fclose(fp);
if ( (fp2 = fopen("2.txt","r")) == NULL )
Edit2->Text = "Fail";
else
Edit2->Text = "OK";
}
if ( uv == 15 )
{
if ( fp2)
fclose(fp2);
Timer1->Enabled = false;
return;
}
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
|
andy7200
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:6 回覆:6 積分:2 註冊:2004-09-27
發送簡訊給我
|
再一次感謝Zard 及blk5743 找到問題了所在了....
加上這一行後 Button1->Enabled = FALSE;
開檔的成功率就百分之百了 謝謝!
|
Zard
尊榮會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
引言:
再一次感謝Zard 及blk5743 找到問題了所在了....
加上這一行後 Button1->Enabled = FALSE;
開檔的成功率就百分之百了 謝謝!
看來你是還沒等程式做完事又在按一次按鈕吧< > 一個檔案只能被打開一次, 在被關閉之前是無法在被開啟的,
若你在檔案被開啟時在去開檔會回傳錯誤值, 要記起來 < >
|