• <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>

            行進中開火

            C++夜未眠

            快速擴展文件大小

            首先轉一段博文:Linux下快速擴展文件大小

             在實際的開發過程中,我們可能需要在文件生成時就立即將文件更改為指定的大小,以便于后續簡化對文件的并發操作,
            這一點在各類的下載工具中有很好的體現。
                要滿足這一需求,通常可以使用的最直觀的方式是:生成文件后,通過循環向文件中寫入任意數據,直到文件大小達到預期的大小。
            對于較小的文件,這種方法可以接受,但是如果文件大小過大,那么這種方法就顯得不夠簡潔且低效。
                這里介紹一種高效且簡單的方法,基于函數"lseek"。
                執行"man lseek"之后,我們可以看到關于lseek特性的關鍵信息,如下:
                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.

            簡單的總結就是:
            1. lseek 函數本身不會擴展文件的大小.
            2. lseek 允許文件的偏移值超過文件的末端,如果下一次在這個偏移點寫入數據,那么在偏移之前的文件末端與偏移點之間的數據將會自動填充為0。

            了解了上面幾點,大家應該想到了擴展文件大小將是一件多么簡潔高效的事情了:)  

            那么,簡單的步驟如下


            1. lseek( iFileHandle , 0 , SEEK_CUR ); //保存文件指針的當前位置以便于在擴展文件大小后恢復到當前位置 2. lseek( iFileHandle , iMoveOffset , SEEK_SET ); 3. write( iFileHandle , " " , WRITE_BYTE_COUNT ); //寫入一個字節的數據,完成對文件大小的更改 4. lseek( iFileHandle , iCurPos , SEEK_SET ); //恢復文件指針到之前保存的文件位置

            實際上,在Windows中的_lseek或者_lseeki64也可以達到相同的目的。MSDN中的描述:

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


            最后,還有一個更好的方法:

            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. 
            這樣一來,截斷和擴展文件都可以用"truncate"來完成,這樣相對于上面的EnlargeFile就更簡潔了而且基于"truncate"的形式只需要一次系統調用即可實現相同的效果,效率上也更有優勢. 

            但是,truncate函數在Windows中不可以使用。可以使用 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 可以用來擴展文件,當一個文件被擴展,被擴展部分,是未被定義的。也就是說,新擴展出來的部分,沒有被付初值,沒有用0填充。

            還需要注意一點,

            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狀態的文件,是不能調用SetEndOfFile的。

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

             

             

             

             

            posted on 2010-03-03 17:57 Jakcie 閱讀(894) 評論(0)  編輯 收藏 引用 所屬分類: C++ & CLinux & QT

            99久久免费国产特黄| 久久人人爽人人爽人人av东京热| 久久天天躁狠狠躁夜夜躁2O2O| 人妻精品久久久久中文字幕69| 99久久中文字幕| 久久最新免费视频| 亚洲精品无码久久久久sm| 精品久久8x国产免费观看| 日韩精品无码久久一区二区三 | 久久久女人与动物群交毛片| 丰满少妇人妻久久久久久| 久久精品国产第一区二区| 无码人妻久久一区二区三区免费 | 亚洲欧美精品一区久久中文字幕| 久久久久国产精品嫩草影院| 国产一区二区精品久久凹凸| 日产精品久久久久久久| 欧美激情精品久久久久久久九九九| 久久久精品国产sm调教网站| 蜜臀久久99精品久久久久久| 久久91综合国产91久久精品| 中文精品久久久久人妻不卡| 欧美伊人久久大香线蕉综合69| 99久久久精品| 狠狠狠色丁香婷婷综合久久俺| 日韩欧美亚洲综合久久| 亚洲一级Av无码毛片久久精品| 91久久九九无码成人网站| 国产高潮国产高潮久久久| 亚洲精品蜜桃久久久久久| 久久精品国产清自在天天线| 久久久久久久国产免费看| 国产精品久久久天天影视香蕉| 国产精品久久久久久久久鸭 | 久久久久久久久久免免费精品| 国产精品一久久香蕉国产线看观看 | 狠色狠色狠狠色综合久久| 久久精品国产亚洲AV麻豆网站 | 一本久久免费视频| 久久夜色精品国产www| 久久免费视频一区|