_chsize

變更檔案的大小。更安全版本為止。 see _chsize_s.

int _chsize( 
   int fd,
   long size 
);

參數

  • fd
    指的已開啟檔案的檔案描述項。

  • size
    新的檔案,以位元組為單位的長度。

傳回值

_chsize如果已成功地變更檔案的大小,則傳回值為 0。傳回值為-1 表示發生錯誤: errno設為EACCES到指定的檔案鎖定對存取, EBADF如果指定的檔案是唯讀的或是描述元不正確, ENOSPC如果沒有可用空間就會留在裝置上,或EINVAL如果size小於零。

請參閱 _doserrno、 errno、 _sys_errlist,以及 _sys_nerr 如需有關這些項目,和其他的詳細資訊,任何傳回碼。

備註

_chsize函數會延伸到或相關聯的檔案會將其截斷fd到所指定的長度size。檔案必須在允許寫入模式開啟。如果該檔案將會擴充,則附加 null 字元 ('\ 0')。如果檔案被截斷中縮短檔案的結尾, 為原始的檔案長度的所有資料都都會遺失。

這個函式會驗證它的參數。如果size小於零或fd是不合法的檔案描述項無效的參數處理常式會叫用,如所述參數驗證

需求

常式

所需的標頭

選擇性標頭

_chsize

<io.h>

<errno.h>

如需相容性資訊,請參閱相容性在簡介中。

範例

// crt_chsize.c
// This program uses _filelength to report the size
// of a file before and after modifying it with _chsize.

#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <share.h>

int main( void )
{
   int fh, result;
   unsigned int nbytes = BUFSIZ;

   // Open a file 
   if( _sopen_s( &fh, "data", _O_RDWR | _O_CREAT, _SH_DENYNO,
                 _S_IREAD | _S_IWRITE ) == 0 )
   {
      printf( "File length before: %ld\n", _filelength( fh ) );
      if( ( result = _chsize( fh, 329678 ) ) == 0 )
         printf( "Size successfully changed\n" );
      else
         printf( "Problem in changing the size\n" );
      printf( "File length after:  %ld\n", _filelength( fh ) );
      _close( fh );
   }
}
  

.NET Framework 對等用法

請參閱

參考

檔案處理

_close

_sopen _wsopen

_open _wopen