問個fstream使用全域變數存檔問題 |
尚未結案
|
swordsman
一般會員 發表:8 回覆:22 積分:6 註冊:2003-03-04 發送簡訊給我 |
請問一下,我想以此方式在Edit1中輸入檔名存檔,哪裡有問題啊?
//--------------------------------------------------------------------
#include
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
我試過了,您的程式,語法上沒有問題 Compile 可以過,但有很大的邏輯問題!
引言: 請問一下,我想以此方式在Edit1中輸入檔名存檔,哪裡有問題啊?改成如下的 code 即可正常存檔//-------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "t.h" #include <iostream.h> #include <iomanip.h> #include <fstream.h> //for file function String file_name; // 這個 file_name 是全域變數,一開始的內容是「空白」的 ofstream outfile(file_name.c_str()); // 這樣指定,在程式一啟動時就已建出 outfile 並指定其檔名了!! // 而其檔名是「空白」因為 file_name 一開始也是空白!! //-------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //-------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //-------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { file_name=Edit1->Text; // 在此指定 file_name 與 outfile 一點關係都沒有,因為在之前已經指定了 String a; a=Memo1->Text; outfile << a.c_str() << endl; Memo2->Text=file_name; //檢查file_name,但為何無字串? // 沒有問題,Memo2 可以顯示出 Edit1 的內容 } //-------------------------------------------------------------------- //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include <iostream.h> #include <iomanip.h> #include <fstream.h> //for file function //----------------------------------------------------------------------------// #pragma package(smart_init) #pragma resource "*.dfm" String file_name; TForm1 *Form1; //----------------------------------------------------------------------------// __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //----------------------------------------------------------------------------// void __fastcall TForm1::Button1Click(TObject *Sender) { file_name=Edit1->Text; String a; a=Memo1->Text; ofstream outfile(file_name.c_str()); // 此時依 file_name 為檔名建立檔案 outfile << a.c_str() << endl; Memo2->Text=file_name; } //----------------------------------------------------------------------------//沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |