|
1 | 1 | ---
|
2 | 2 | title: "_popen, _wpopen"
|
3 | 3 | 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" |
5 | 5 | api_name: ["_popen", "_wpopen", "_o__popen", "_o__wpopen"]
|
6 | 6 | 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"]
|
7 | 7 | api_type: ["DLLExport"]
|
8 | 8 | topic_type: ["apiref"]
|
9 | 9 | f1_keywords: ["tpopen", "popen", "wpopen", "_popen", "_wpopen", "_tpopen"]
|
10 | 10 | helpviewer_keywords: ["tpopen function", "pipes, creating", "_popen function", "_tpopen function", "popen function", "wpopen function", "_wpopen function"]
|
11 |
| -ms.assetid: eb718ff2-c87d-4bd4-bd2e-ba317c3d6973 |
12 | 11 | no-loc: [_popen, _wpopen, _tpopen, _doserrno, errno, _sys_errlist, _sys_nerr, EINVAL]
|
13 | 12 | ---
|
14 | 13 | # `_popen`, `_wpopen`
|
@@ -85,44 +84,47 @@ All versions of the [C run-time libraries](../crt-library-features.md).
|
85 | 84 | ## Example
|
86 | 85 |
|
87 | 86 | ```C
|
88 |
| -// crt_popen.c |
| 87 | +// popen.c |
89 | 88 | /* This program uses _popen and _pclose to receive a
|
90 | 89 | * stream of text from a system process.
|
91 | 90 | */
|
92 | 91 |
|
93 | 92 | #include <stdio.h>
|
94 | 93 | #include <stdlib.h>
|
95 | 94 |
|
96 |
| -int main( void ) |
| 95 | +int main(void) |
97 | 96 | {
|
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 | + } |
126 | 128 | }
|
127 | 129 | ```
|
128 | 130 |
|
|
0 commit comments