• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            行進(jìn)中開火

            C++夜未眠

            快速擴(kuò)展文件大小

            首先轉(zhuǎn)一段博文:Linux下快速擴(kuò)展文件大小

             在實(shí)際的開發(fā)過程中,我們可能需要在文件生成時(shí)就立即將文件更改為指定的大小,以便于后續(xù)簡(jiǎn)化對(duì)文件的并發(fā)操作,
            這一點(diǎn)在各類的下載工具中有很好的體現(xiàn)。
                要滿足這一需求,通??梢允褂玫淖钪庇^的方式是:生成文件后,通過循環(huán)向文件中寫入任意數(shù)據(jù),直到文件大小達(dá)到預(yù)期的大小。
            對(duì)于較小的文件,這種方法可以接受,但是如果文件大小過大,那么這種方法就顯得不夠簡(jiǎn)潔且低效。
                這里介紹一種高效且簡(jiǎn)單的方法,基于函數(shù)"lseek"。
                執(zhí)行"man lseek"之后,我們可以看到關(guān)于lseek特性的關(guān)鍵信息,如下:
                The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. 
                If data is later written at this point, subsequent reads of data in the gap shall return bytes with the 
                value 0 until data is actually written into the gap.
                The lseek() function shall not, by itself, extend the size of a file.

            簡(jiǎn)單的總結(jié)就是:
            1. lseek 函數(shù)本身不會(huì)擴(kuò)展文件的大小.
            2. lseek 允許文件的偏移值超過文件的末端,如果下一次在這個(gè)偏移點(diǎn)寫入數(shù)據(jù),那么在偏移之前的文件末端與偏移點(diǎn)之間的數(shù)據(jù)將會(huì)自動(dòng)填充為0。

            了解了上面幾點(diǎn),大家應(yīng)該想到了擴(kuò)展文件大小將是一件多么簡(jiǎn)潔高效的事情了:)  

            那么,簡(jiǎn)單的步驟如下


            1. lseek( iFileHandle , 0 , SEEK_CUR ); //保存文件指針的當(dāng)前位置以便于在擴(kuò)展文件大小后恢復(fù)到當(dāng)前位置 2. lseek( iFileHandle , iMoveOffset , SEEK_SET ); 3. write( iFileHandle , " " , WRITE_BYTE_COUNT ); //寫入一個(gè)字節(jié)的數(shù)據(jù),完成對(duì)文件大小的更改 4. lseek( iFileHandle , iCurPos , SEEK_SET ); //恢復(fù)文件指針到之前保存的文件位置

            實(shí)際上,在Windows中的_lseek或者_(dá)lseeki64也可以達(dá)到相同的目的。MSDN中的描述:

            You can use _lseek to reposition the pointer anywhere in a file or beyond the end of the file.


            最后,還有一個(gè)更好的方法:

            int truncate(const char *path, off_t length); 
            If the file previously was larger than length, the extra data is discarded. 
            If the file was previously shorter than length, its size is increased, and the extended area appears as if it were zero-filled. 
            這樣一來,截?cái)嗪蛿U(kuò)展文件都可以用"truncate"來完成,這樣相對(duì)于上面的EnlargeFile就更簡(jiǎn)潔了而且基于"truncate"的形式只需要一次系統(tǒng)調(diào)用即可實(shí)現(xiàn)相同的效果,效率上也更有優(yōu)勢(shì). 

            但是,truncate函數(shù)在Windows中不可以使用??梢允褂?nbsp;SetEndOfFile 來代替。

             

            Truncating or Extending Files

            An application can truncate or extend a file by calling SetEndOfFile on sets the end-of-file marker to the current position of the file pointer.

            Note that when a file is extended, the contents between the old and new end-of-file locations are not defined.

             

            The SetEndOfFile function can be used to truncate or extend a file. If the file is extended, the contents of the file between the old end of the file and the new end of the file are not defined.

            SetEndOfFile 可以用來擴(kuò)展文件,當(dāng)一個(gè)文件被擴(kuò)展,被擴(kuò)展部分,是未被定義的。也就是說,新擴(kuò)展出來的部分,沒有被付初值,沒有用0填充。

            還需要注意一點(diǎn),

            If CreateFileMapping is called to create a file mapping object for hFileUnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

            也就是說,在Mapping狀態(tài)的文件,是不能調(diào)用SetEndOfFile的。

            MSDN鏈接:http://msdn.microsoft.com/en-us/library/aa365531(VS.85).aspx

             

             

             

             

            posted on 2010-03-03 17:57 Jakcie 閱讀(883) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ & C 、Linux & QT

            久久WWW免费人成—看片| 亚洲国产高清精品线久久| 久久久亚洲AV波多野结衣| 久久精品国产亚洲AV忘忧草18| 精品人妻伦九区久久AAA片69| 激情综合色综合久久综合| 亚洲精品乱码久久久久久蜜桃不卡 | 久久香蕉国产线看观看精品yw| 国产成人久久精品激情| 久久精品一区二区影院 | 久久国产乱子精品免费女| 久久精品国产WWW456C0M| 精品久久久久久久久免费影院 | 久久久久国产精品三级网| 中文字幕乱码久久午夜| 国内精品久久久久久久久| 亚洲国产欧洲综合997久久| 久久国产视频99电影| 亚洲精品白浆高清久久久久久 | 久久久久亚洲精品无码网址| 亚洲AV无码久久寂寞少妇| 久久久久亚洲精品中文字幕 | 久久天天婷婷五月俺也去| 久久久青草青青亚洲国产免观 | 国产激情久久久久影院小草| 色欲久久久天天天综合网| 亚洲另类欧美综合久久图片区| 国产精品九九九久久九九| 亚洲精品无码成人片久久| 日韩欧美亚洲综合久久| 无码任你躁久久久久久久| 香港aa三级久久三级| 国内精品久久久久久野外| 久久精品人人做人人妻人人玩| 日日狠狠久久偷偷色综合0| 久久久久人妻一区精品果冻| 国产 亚洲 欧美 另类 久久| 99久久婷婷国产综合精品草原| 久久亚洲国产精品一区二区| 国产成人久久激情91| 久久国产精品成人免费|