請問檔案要存在不同路徑的問題 |
答題得分者是:jow
|
ladese05
一般會員 發表:3 回覆:2 積分:1 註冊:2006-09-13 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
簡單測試碼,提供你參考...
String __fastcall TForm1::GetFileName(int sub0, int sub1, int fileno); 取得檔名並檢查目錄是否存在, 同時建立不存在的目錄 [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); String __fastcall GetFileName(int sub0, int sub1, int fileno); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" #include "dir.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { randomize(); } //--------------------------------------------------------------------------- String __fastcall TForm1::GetFileName(int sub0, int sub1, int fileno) {//取得檔名並檢查目錄是否存在, 同時建立不存在的目錄 String fn, dir, sub; fn.sprintf("TESTDIR\\%3.3d\\%3.3d\\%3.3d.DAT",sub0,sub1,fileno); dir = ExtractFileDir(fn); if(!DirectoryExists(dir)){ for(int i=1; i sub=dir.SubString(1,i); if(!DirectoryExists(sub)) mkdir(sub.c_str()); mkdir(dir.c_str()); } } } return fn; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { TMemoryStream *M = new TMemoryStream(); try{ for(int i=0; i<100; i ){ int sub0 = random(100) 1; int sub1 = random(100) 1; int fileno = random(100) 1; String fn = GetFileName(sub0, sub1, fileno); M->SaveToFile(fn); } } __finally{ delete M; } } //--------------------------------------------------------------------------- [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |