全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:773
推到 Plurk!
推到 Facebook!

程式的compile沒有問題下,Run有時成功有時失敗,有可能嗎?

尚未結案
andy7200
一般會員


發表:6
回覆:6
積分:2
註冊:2004-09-27

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-10-23 18:30:18 IP:218.166.xxx.xxx 未訂閱
原則上,程式應該是每次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
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-10-24 11:42:36 IP:61.64.xxx.xxx 未訂閱
老實說你的程式怪怪的, 不建議把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
一般會員


發表:6
回覆:6
積分:2
註冊:2004-09-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-10-26 09:52:44 IP:163.29.xxx.xxx 未訂閱
感謝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
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-10-26 12:10:10 IP:211.23.xxx.xxx 未訂閱
怪了, 我怎麼試不出你說的問題呢? 你的程式碼全都有貼上來嗎? 還是這只是部份的碼呢?    我修改了一下, 在貼一次    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
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-10-26 14:05:50 IP:61.66.xxx.xxx 未訂閱
參考一下,我剛試過了,是可行的    
#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
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-10-26 14:07:31 IP:61.66.xxx.xxx 未訂閱
參考一下,我剛試過了,是可行的    
#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
一般會員


發表:6
回覆:6
積分:2
註冊:2004-09-27

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-10-26 18:59:21 IP:218.166.xxx.xxx 未訂閱
再一次感謝Zard 及blk5743 找到問題了所在了.... 加上這一行後 Button1->Enabled = FALSE; 開檔的成功率就百分之百了 謝謝!
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-10-26 20:26:46 IP:61.64.xxx.xxx 未訂閱
引言: 再一次感謝Zard 及blk5743 找到問題了所在了.... 加上這一行後 Button1->Enabled = FALSE; 開檔的成功率就百分之百了 謝謝!
看來你是還沒等程式做完事又在按一次按鈕吧< > 一個檔案只能被打開一次, 在被關閉之前是無法在被開啟的, 若你在檔案被開啟時在去開檔會回傳錯誤值, 要記起來 < >
系統時間:2024-06-29 15:17:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!