|
1 | 1 | ---
|
2 | 2 | title: "fflush"
|
3 |
| -ms.date: "11/04/2016" |
| 3 | +ms.date: "09/11/2019" |
4 | 4 | apiname: ["fflush"]
|
5 | 5 | apilocation: ["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"]
|
6 | 6 | apitype: "DLLExport"
|
@@ -56,44 +56,50 @@ For additional compatibility information, see [Compatibility](../../c-runtime-li
|
56 | 56 |
|
57 | 57 | ```C
|
58 | 58 | // crt_fflush.c
|
| 59 | +// Compile with: cl /W4 crt_fflush.c |
| 60 | +// This sample gets a number from the user, then writes it to a file. |
| 61 | +// It ensures the write isn't lost on crash by calling fflush. |
59 | 62 | #include <stdio.h>
|
60 |
| -#include <conio.h> |
61 | 63 |
|
62 |
| -int main( void ) |
| 64 | +int * crash_the_program = 0; |
| 65 | + |
| 66 | +int main(void) |
63 | 67 | {
|
64 |
| - int integer; |
65 |
| - char string[81]; |
66 |
| - |
67 |
| - // Read each word as a string. |
68 |
| - printf( "Enter a sentence of four words with scanf: " ); |
69 |
| - for( integer = 0; integer < 4; integer++ ) |
70 |
| - { |
71 |
| - scanf_s( "%s", string, sizeof(string) ); |
72 |
| - printf( "%s\n", string ); |
73 |
| - } |
74 |
| - |
75 |
| - // You must flush the input buffer before using gets. |
76 |
| - // fflush on input stream is an extension to the C standard |
77 |
| - fflush( stdin ); |
78 |
| - printf( "Enter the same sentence with gets: " ); |
79 |
| - gets_s( string, sizeof(string) ); |
80 |
| - printf( "%s\n", string ); |
| 68 | + FILE * my_file; |
| 69 | + errno_t err = fopen_s(&my_file, "myfile.txt", "w"); |
| 70 | + if (my_file && !err) |
| 71 | + { |
| 72 | + printf("Write a number: "); |
| 73 | + |
| 74 | + int my_number = 0; |
| 75 | + scanf_s("%d", &my_number); |
| 76 | + |
| 77 | + fprintf(my_file, "User selected %d\n", my_number); |
| 78 | + |
| 79 | + // Write data to a file immediately instead of buffering. |
| 80 | + fflush(my_file); |
| 81 | + |
| 82 | + if (my_number == 5) |
| 83 | + { |
| 84 | + // Without using fflush, no data was written to the file |
| 85 | + // prior to the crash, so the data is lost. |
| 86 | + *crash_the_program = 5; |
| 87 | + } |
| 88 | + |
| 89 | + // Normally, fflush is not needed as closing the file will write the buffer. |
| 90 | + // Note that files are automatically closed and flushed during normal termination. |
| 91 | + fclose(my_file); |
| 92 | + } |
| 93 | + return 0; |
81 | 94 | }
|
82 | 95 | ```
|
83 | 96 |
|
84 | 97 | ```Input
|
85 |
| -This is a test |
86 |
| -This is a test |
| 98 | +5 |
87 | 99 | ```
|
88 | 100 |
|
89 |
| -```Output |
90 |
| -Enter a sentence of four words with scanf: This is a test |
91 |
| -This |
92 |
| -is |
93 |
| -a |
94 |
| -test |
95 |
| -Enter the same sentence with gets: This is a test |
96 |
| -This is a test |
| 101 | +```myfile.txt |
| 102 | +User selected 5 |
97 | 103 | ```
|
98 | 104 |
|
99 | 105 | ## See also
|
|
0 commit comments