Skip to content

Commit e69064f

Browse files
author
Colin Robertson
authored
Merge pull request #703 from MicrosoftDocs/FromPublicMasterBranch
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master)
2 parents a86a763 + f56503a commit e69064f

1 file changed

Lines changed: 88 additions & 87 deletions

File tree

Lines changed: 88 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22
title: "_chsize | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "11/04/2016"
5-
ms.reviewer: ""
6-
ms.suite: ""
4+
ms.date: "03/29/2018"
75
ms.technology: ["cpp-standard-libraries"]
86
ms.tgt_pltfrm: ""
97
ms.topic: "reference"
@@ -14,91 +12,94 @@ f1_keywords: ["_chsize"]
1412
dev_langs: ["C++"]
1513
helpviewer_keywords: ["size", "_chsize function", "size, changing file", "files [C++], changing size", "chsize function"]
1614
ms.assetid: b3e881c5-7b27-4837-a3d4-c51591ab10ff
17-
caps.latest.revision: 21
1815
author: "corob-msft"
1916
ms.author: "corob"
2017
manager: "ghogen"
2118
ms.workload: ["cplusplus"]
22-
---
23-
# _chsize
24-
Changes the size of a file. A more secure version is available; see [_chsize_s](../../c-runtime-library/reference/chsize-s.md).
25-
26-
## Syntax
27-
28-
```
29-
int _chsize(
30-
int fd,
31-
long size
32-
);
33-
```
34-
35-
#### Parameters
36-
`fd`
37-
File descriptor referring to an open file.
38-
39-
`size`
40-
New length of the file in bytes.
41-
42-
## Return Value
43-
`_chsize` returns the value 0 if the file size is successfully changed. A return value of -1 indicates an error: `errno` is set to `EACCES` if the specified file is locked against access, to `EBADF` if the specified file is read-only or the descriptor is invalid, `ENOSPC` if no space is left on the device, or `EINVAL` if `size` is less than zero.
44-
45-
See [_doserrno, errno, _sys_errlist, and _sys_nerr](../../c-runtime-library/errno-doserrno-sys-errlist-and-sys-nerr.md) for more information on these, and other, return codes.
46-
47-
## Remarks
48-
The `_chsize` function extends or truncates the file associated with `fd` to the length specified by `size`. The file must be open in a mode that permits writing. Null characters ('\0') are appended if the file is extended. If the file is truncated, all data from the end of the shortened file to the original length of the file is lost.
49-
50-
This function validates its parameters. If `size` is less than zero or `fd` is a bad file descriptor, the invalid parameter handler is invoked, as described in [Parameter Validation](../../c-runtime-library/parameter-validation.md).
51-
52-
## Requirements
53-
54-
|Routine|Required header|Optional header|
55-
|-------------|---------------------|---------------------|
56-
|`_chsize`|\<io.h>|\<errno.h>|
57-
58-
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md) in the Introduction.
59-
60-
## Example
61-
62-
```
63-
// crt_chsize.c
64-
// This program uses _filelength to report the size
65-
// of a file before and after modifying it with _chsize.
66-
67-
#include <io.h>
68-
#include <fcntl.h>
69-
#include <sys/types.h>
70-
#include <sys/stat.h>
71-
#include <stdio.h>
72-
#include <share.h>
73-
74-
int main( void )
75-
{
76-
int fh, result;
77-
unsigned int nbytes = BUFSIZ;
78-
79-
// Open a file
80-
if( _sopen_s( &fh, "data", _O_RDWR | _O_CREAT, _SH_DENYNO,
81-
_S_IREAD | _S_IWRITE ) == 0 )
82-
{
83-
printf( "File length before: %ld\n", _filelength( fh ) );
84-
if( ( result = _chsize( fh, 329678 ) ) == 0 )
85-
printf( "Size successfully changed\n" );
86-
else
87-
printf( "Problem in changing the size\n" );
88-
printf( "File length after: %ld\n", _filelength( fh ) );
89-
_close( fh );
90-
}
91-
}
92-
```
93-
94-
```Output
95-
File length before: 0
96-
Size successfully changed
97-
File length after: 329678
98-
```
99-
100-
## See Also
101-
[File Handling](../../c-runtime-library/file-handling.md)
102-
[_close](../../c-runtime-library/reference/close.md)
103-
[_sopen, _wsopen](../../c-runtime-library/reference/sopen-wsopen.md)
104-
[_open, _wopen](../../c-runtime-library/reference/open-wopen.md)
19+
---
20+
# _chsize
21+
22+
Changes the size of a file. A more secure version is available; see [_chsize_s](../../c-runtime-library/reference/chsize-s.md).
23+
24+
## Syntax
25+
26+
```C
27+
int _chsize(
28+
int fd,
29+
long size
30+
);
31+
```
32+
33+
### Parameters
34+
*fd*<br/>
35+
File descriptor referring to an open file.
36+
37+
*size*<br/>
38+
New length of the file in bytes.
39+
40+
## Return Value
41+
42+
_chsize` returns the value 0 if the file size is successfully changed. A return value of -1 indicates an error: `errno` is set to `EACCES` if the specified file is read-only or the specified file is locked against access, to `EBADF` if the descriptor is invalid, `ENOSPC` if no space is left on the device, or `EINVAL` if `size` is less than zero.
43+
44+
See [_doserrno, errno, _sys_errlist, and _sys_nerr](../../c-runtime-library/errno-doserrno-sys-errlist-and-sys-nerr.md) for more information on these, and other, return codes.
45+
46+
## Remarks
47+
48+
The `_chsize` function extends or truncates the file associated with `fd` to the length specified by `size`. The file must be open in a mode that permits writing. Null characters ('\0') are appended if the file is extended. If the file is truncated, all data from the end of the shortened file to the original length of the file is lost.
49+
50+
This function validates its parameters. If `size` is less than zero or `fd` is a bad file descriptor, the invalid parameter handler is invoked, as described in [Parameter Validation](../../c-runtime-library/parameter-validation.md).
51+
52+
## Requirements
53+
54+
|Routine|Required header|Optional header|
55+
|-------------|---------------------|---------------------|
56+
|`_chsize`|\<io.h>|\<errno.h>|
57+
58+
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
59+
60+
## Example
61+
62+
```C
63+
// crt_chsize.c
64+
// This program uses _filelength to report the size
65+
// of a file before and after modifying it with _chsize.
66+
67+
#include <io.h>
68+
#include <fcntl.h>
69+
#include <sys/types.h>
70+
#include <sys/stat.h>
71+
#include <stdio.h>
72+
#include <share.h>
73+
74+
int main( void )
75+
{
76+
int fh, result;
77+
unsigned int nbytes = BUFSIZ;
78+
79+
// Open a file
80+
if( _sopen_s( &fh, "data", _O_RDWR | _O_CREAT, _SH_DENYNO,
81+
_S_IREAD | _S_IWRITE ) == 0 )
82+
{
83+
printf( "File length before: %ld\n", _filelength( fh ) );
84+
if( ( result = _chsize( fh, 329678 ) ) == 0 )
85+
printf( "Size successfully changed\n" );
86+
else
87+
printf( "Problem in changing the size\n" );
88+
printf( "File length after: %ld\n", _filelength( fh ) );
89+
_close( fh );
90+
}
91+
}
92+
```
93+
94+
```Output
95+
File length before: 0
96+
Size successfully changed
97+
File length after: 329678
98+
```
99+
100+
## See also
101+
102+
[File Handling](../../c-runtime-library/file-handling.md)<br/>
103+
[_close](../../c-runtime-library/reference/close.md)<br/>
104+
[_sopen, _wsopen](../../c-runtime-library/reference/sopen-wsopen.md)<br/>
105+
[_open, _wopen](../../c-runtime-library/reference/open-wopen.md)<br/>

0 commit comments

Comments
 (0)