線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1628
推到 Plurk!
推到 Facebook!

TMemoryStream 的問題

尚未結案
yaozong
一般會員


發表:5
回覆:7
積分:2
註冊:2004-10-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-11 01:48:56 IP:61.59.xxx.xxx 未訂閱
小弟正在寫小畫家的程式,期中某項功能就是把圖形物件化,並能夠存檔 後,再下次開起時也能對上次所畫的圖形作修改,我的做法是把所有的圖存入 一個List中,並在存檔時寫入MemoryStream裡,現在有個問題就是 要如何讀出MemoryStream理已經存好的資料並寫回List裡呢? 以下的存入MemoryStream的code  
 TMemoryStream* memorystream= new TMemoryStream();
                for(int i=0;iCount;i  )
                {
                        memorystream->Write(List->Items[i],sizeof(List->Items[i]));
                }
                memorystream->SaveToFile("asd.chr");    
                memorystream->Position=0;
                delete memorystream;
其中memorystream->SaveToFile("asd.chr");這行是看站上大大寫的一些範例 其功能 能否告知呢?
allenchan
資深會員


發表:10
回覆:306
積分:283
註冊:2004-01-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-11 02:23:30 IP:61.230.xxx.xxx 未訂閱
您對 TMemoryStream 所有的操作動作都會是在 Memory 中進行,當你呼叫 SaveToFile 時,就會將存放在 Memory 的資料儲存到檔案裡面。    相對的有 SaveToFile 就該有 LoadFromFile,可讓您將存放在檔案的資料 Load 進 Memory 中來做處理。    以下是 TMemoryStream 的 Description Use TMemoryStream to store data in a dynamic memory buffer that is enhanced with file-like access capabilities. TMemoryStream provides the general I/O capabilities of a stream object while introducing methods and properties to manage a dynamic memory buffer. 發表人 - allenchan 於 2004/12/11 02:25:09
yaozong
一般會員


發表:5
回覆:7
積分:2
註冊:2004-10-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-11 03:10:40 IP:61.59.xxx.xxx 未訂閱
可在請問那要如何讀出檔案裡面的資料呢? 我只知道有read和write這兩個可以用!! 而read的函式中Read(void *Buffer, int Count) 所謂的Buffer 為何? Count 又是什麼呢?
allenchan
資深會員


發表:10
回覆:306
積分:283
註冊:2004-01-06

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-11 05:08:28 IP:61.230.xxx.xxx 未訂閱
讀取檔案的部份如下: memorystream->LoadFromFile("asd.chr");    至於 Read 的說明,多按一下 F1 可以得到很多資訊喔!    TCustomMemoryStream::Read Reads up to Count bytes from the memory stream into Buffer and advances the current position of the stream by the number of bytes read. virtual int __fastcall Read(void *Buffer, int Count); Description Use Read to read the contents of the memory stream into a buffer, starting at the current position. Read will read up to Count bytes from the current position in Memory. If Count bytes extends beyond the end of the memory buffer, Read will only transfer the data up to the end of the associated memory buffer. Read returns the number of bytes actually transferred to Buffer, and advances the current position accordingly. If the return value is less than Count, it means that reading reached the end of the stream data. All the other data-reading methods of the memory stream (ReadBuffer, ReadComponent) call Read to do the actual reading. Note: Read treats Count as an upper bound. The ReadBuffer method, by contrast, throws an exception if Count bytes cannot be read. 發表人 - allenchan 於 2004/12/11 05:19:55
yaozong
一般會員


發表:5
回覆:7
積分:2
註冊:2004-10-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-11 14:32:11 IP:61.59.xxx.xxx 未訂閱
不好意思在請問一下,我的code寫成下列那樣:  
                TMemoryStream* memorystream= new TMemoryStream();
                memorystream->Position=0;
                for(int i=0;iCount;i  )
                {
                        memorystream->Write(List->Items[i],sizeof(List->Items[i]));
                }
                memorystream->SaveToFile("asd.chr");
                delete memorystream;
//--------------------------------------------------------------
        TMemoryStream* memorystream = new TMemoryStream() ;
        memorystream->LoadFromFile("asd.chr");
        for(int i=0;List->Count;i  )
        {
                memorystream->Read(List->Items[i],sizeof(List->Items[i]));
        }
        ReDraw(List);//我自行定義的函式,重繪LIst裡所有的圖形
        delete memorystream;
 
他並沒有重劃List裡的圖,請問各位大大們,是哪裡有錯呢? 發表人 - yaozong 於 2004/12/11 14:34:36
allenchan
資深會員


發表:10
回覆:306
積分:283
註冊:2004-01-06

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-12-12 16:47:29 IP:61.62.xxx.xxx 未訂閱
我想您這樣的儲存方式是無法將您的圖形存到檔案中的: memorystream->Write(List->Items[i],sizeof(List->Items[i])); 這段程式碼只會將 List->Items[i] 內容寫入 sizeof(List->Items[i]) 到 memorystream 裡頭,但是 sizeof(List->Items[i]) 的結果只會等於 4 吧? List->Items[i] 只是一個指標儲存一個位址,sizeof 它是沒法得到實際的大小哩。 您要儲存的應該是 List->Items[i] 所指到的物件才對: http://delphi.ktop.com.tw/topic.php?topic_id=43842 發表人 - allenchan 於 2004/12/12 16:49:38
系統時間:2024-06-28 18:31:36
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!