Skip to content

Commit dfd2a93

Browse files
authored
Merge pull request #4761 from TylerMSFT/github4384
fix code example to close pipe in all cases
2 parents ec6e9b1 + 9b7b60d commit dfd2a93

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

docs/c-runtime-library/reference/popen-wpopen.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
title: "_popen, _wpopen"
33
description: "A reference for the Microsoft C runtime (CRT) library functions _popen and _wpopen."
4-
ms.date: "4/2/2020"
4+
ms.date: "1/25/2023"
55
api_name: ["_popen", "_wpopen", "_o__popen", "_o__wpopen"]
66
api_location: ["msvcrt.dll", "msvcr80.dll", "msvcr90.dll", "msvcr100.dll", "msvcr100_clr0400.dll", "msvcr110.dll", "msvcr110_clr0400.dll", "msvcr120.dll", "msvcr120_clr0400.dll", "ucrtbase.dll", "api-ms-win-crt-stdio-l1-1-0.dll"]
77
api_type: ["DLLExport"]
88
topic_type: ["apiref"]
99
f1_keywords: ["tpopen", "popen", "wpopen", "_popen", "_wpopen", "_tpopen"]
1010
helpviewer_keywords: ["tpopen function", "pipes, creating", "_popen function", "_tpopen function", "popen function", "wpopen function", "_wpopen function"]
11-
ms.assetid: eb718ff2-c87d-4bd4-bd2e-ba317c3d6973
1211
no-loc: [_popen, _wpopen, _tpopen, _doserrno, errno, _sys_errlist, _sys_nerr, EINVAL]
1312
---
1413
# `_popen`, `_wpopen`
@@ -85,44 +84,47 @@ All versions of the [C run-time libraries](../crt-library-features.md).
8584
## Example
8685

8786
```C
88-
// crt_popen.c
87+
// popen.c
8988
/* This program uses _popen and _pclose to receive a
9089
* stream of text from a system process.
9190
*/
9291

9392
#include <stdio.h>
9493
#include <stdlib.h>
9594

96-
int main( void )
95+
int main(void)
9796
{
98-
99-
char psBuffer[128];
100-
FILE *pPipe;
101-
102-
/* Run DIR so that it writes its output to a pipe. Open this
103-
* pipe with read text attribute so that we can read it
104-
* like a text file.
105-
*/
106-
107-
if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
108-
exit( 1 );
109-
110-
/* Read pipe until end of file, or an error occurs. */
111-
112-
while(fgets(psBuffer, 128, pPipe))
113-
{
114-
puts(psBuffer);
115-
}
116-
117-
/* Close pipe and print return value of pPipe. */
118-
if (feof( pPipe))
119-
{
120-
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
121-
}
122-
else
123-
{
124-
printf( "Error: Failed to read the pipe to the end.\n");
125-
}
97+
char psBuffer[128];
98+
FILE* pPipe;
99+
100+
/* Run DIR so that it writes its output to a pipe. Open this
101+
* pipe with read text attribute so that we can read it
102+
* like a text file.
103+
*/
104+
105+
if ((pPipe = _popen("dir *.c /on /p", "rt")) == NULL)
106+
{
107+
exit(1);
108+
}
109+
110+
/* Read pipe until end of file, or an error occurs. */
111+
112+
while (fgets(psBuffer, 128, pPipe))
113+
{
114+
puts(psBuffer);
115+
}
116+
117+
int endOfFileVal = feof(pPipe);
118+
int closeReturnVal = _pclose(pPipe);
119+
120+
if (endOfFileVal)
121+
{
122+
printf("\nProcess returned %d\n", closeReturnVal);
123+
}
124+
else
125+
{
126+
printf("Error: Failed to read the pipe to the end.\n");
127+
}
126128
}
127129
```
128130

0 commit comments

Comments
 (0)