Stream I/O

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see Stream I-O on docs.microsoft.com. These functions process data in different sizes and formats, from single characters to large data structures. They also provide buffering, which can improve performance. The default size of a stream buffer is 4K. These routines affect only buffers created by the run-time library routines, and have no effect on buffers created by the operating system.

Stream I/O Routines

RoutineUse.NET Framework equivalent
clearerr, clearerr_sClear error indicator for streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
fcloseClose streamSystem::IO::Stream::Close, System::IO::BinaryReader::Close, System::IO::BinaryWriter::Close, System::IO::TextReader::Close, System::IO::TextWriter::Close, System::IO::StringReader::Close, System::IO::StringWriter::Close, System::IO::StreamReader::Close, System::IO::StreamWriter::Close
_fcloseallClose all open streams except stdin, stdout, and stderrSystem::IO::Stream::Close, System::IO::BinaryReader::Close, System::IO::BinaryWriter::Close, System::IO::TextReader::Close, System::IO::TextWriter::Close, System::IO::StringReader::Close, System::IO::StringWriter::Close, System::IO::StreamReader::Close, System::IO::StreamWriter::Close
_fdopen, wfdopenAssociate stream with file descriptor of open fileFileStream
feofTest for end of file on streamSystem::IO::FileStream::Read
ferrorTest for error on streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
fflushFlush stream to buffer or storage deviceSystem::IO::FileStream::Flush
fgetc, fgetwcRead character from stream (function versions of getc and getwc)System::IO::StreamReader::Read
_fgetchar, _fgetwcharRead character from stdin (function versions of getchar and getwchar)System::Console::Read
fgetposGet position indicator of streamSystem::IO::FileStream::Position
fgets, fgetwsRead string from streamSystem::IO::StreamReader::ReadLine, System::IO::TextReader::ReadBlock
_filenoGet file descriptor associated with streamSystem::IO::FileStream::Handle
_flushallFlush all streams to buffer or storage deviceSystem::IO::FileStream::Flush, System::IO::StreamWriter::Flush, System::IO::TextWriter::Flush, System::IO::BinaryWriter::Flush
fopen, _wfopen, fopen_s, _wfopen_sOpen streamSystem::IO::File::Open
fprintf, _fprintf_l, fwprintf, _fwprintf_l, fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_lWrite formatted data to streamSystem::IO::StreamWriter::Write
fputc, fputwcWrite a character to a stream (function versions of putc and putwc)System::IO::StreamWriter::Write
_fputchar, _fputwcharWrite character to stdout (function versions of putchar and putwchar)System::Console::Write
fputs, fputwsWrite string to streamSystem::IO::StreamWriter::Write
freadRead unformatted data from streamSystem::IO::FileStream::Read
freopen, _wfreopen, freopen_s, _wfreopen_sReassign FILE stream pointer to new file or deviceSystem::IO::File::Open
fscanf, fwscanf, fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_lRead formatted data from streamSystem::IO::StreamReader::ReadLine; see also Parse methods, such as System::Double::Parse.
fseek, _fseeki64Move file position to given locationSystem::IO::FileStream::Position, System::IO::FileStream::Seek
fsetposSet position indicator of streamSystem::IO::FileStream::Position
_fsopen, _wfsopenOpen stream with file sharingNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
ftell, _ftelli64Get current file positionSystem::IO::FileStream::Position
fwriteWrite unformatted data items to streamSystem::IO::FileStream::Write
getc, getwcRead character from stream (macro versions of fgetc and fgetwc)System::IO::StreamReader::Read
getchar, getwcharRead character from stdin (macro versions of fgetchar and fgetwchar)System::Console::Read
_getmaxstdioReturns the number of simultaneously open files permitted at the stream I/O level.Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
gets_s, _getws_sRead line from stdinSystem::Console::Read
_getwRead binary int from streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
printf, _printf_l, wprintf, _wprintf_l,printf_s, _printf_s_l, wprintf_s, _wprintf_s_lWrite formatted data to stdoutSystem::Console::Write
putc, putwcWrite character to a stream (macro versions of fputc and fputwc)System::IO::StreamWriter::Write
putchar, putwcharWrite character to stdout (macro versions of fputchar and fputwchar)System::Console::Write
puts, _putwsWrite line to streamSystem::Console::Write
_putwWrite binary int to streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
rewindMove file position to beginning of streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_rmtmpRemove temporary files created by tmpfileNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
scanf, _scanf_l, wscanf, _wscanf_l,scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_lRead formatted data from stdinSystem::Console::ReadLine; see also Parse methods, such as System::Double::Parse.
setbufControl stream bufferingNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_setmaxstdioSet a maximum for the number of simultaneously open files at the stream I/O level.Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
setvbufControl stream buffering and buffer sizeNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_snprintf, _snwprintf, _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_lWrite formatted data of specified length to stringNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_snscanf, _snwscanf, _snscanf_s, _snscanf_s_l, _snwscanf_s, _snwscanf_s_lRead formatted data of a specified length from the standard input stream.Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
sprintf, swprintf, sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_lWrite formatted data to stringSystem::String::Format
sscanf, swscanf, sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_lRead formatted data from stringSee Parse methods, such as System::Double::Parse
_tempnam, _wtempnamGenerate temporary filename in given directoryNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
tmpfile, tmpfile_sCreate temporary fileNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
tmpnam, _wtmpnam, tmpnam_s, _wtmpnam_sGenerate temporary filenameNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
ungetc, ungetwcPush character back onto streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_vcprintf, _vcwprintf, _vcprintf_s, _vcprintf_s_l, _vcwprintf_s, _vcwprintf_s_lWrite formatted data to the console.System::Console::Write
vfprintf, vfwprintf, vfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_lWrite formatted data to streamNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
vprintf, vwprintf, vprintf_s, _vprintf_s_l, vwprintf_s, _vwprintf_s_lWrite formatted data to stdoutSystem::Console::Write
_vsnprintf, _vsnwprintf, vsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_lWrite formatted data of specified length to bufferNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
vsprintf, vswprintf, vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_lWrite formatted data to bufferSystem::String::Format

When a program begins execution, the startup code automatically opens several streams: standard input (pointed to by stdin), standard output (pointed to by stdout), and standard error (pointed to by stderr). These streams are directed to the console (keyboard and screen) by default. Use freopen to redirect stdin, stdout, or stderr to a disk file or a device.

Files opened using the stream routines are buffered by default. The stdout and stderr functions are flushed whenever they are full or, if you are writing to a character device, after each library call. If a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. Use fflush or _flushall to ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writing it to disk. The commit-to-disk feature ensures that the flushed buffer contents are not lost in the event of a system failure.

There are two ways to commit buffer contents to disk:

  • Link with the file COMMODE.OBJ to set a global commit flag. The default setting of the global flag is n, for "no-commit."

  • Set the mode flag to c with fopen or _fdopen.

Any file specifically opened with either the c or the n flag behaves according to the flag, regardless of the state of the global commit/no-commit flag.

If your program does not explicitly close a stream, the stream is automatically closed when the program terminates. However, you should close a stream when your program finishes with it, as the number of streams that can be open at one time is limited. See _setmaxstdio for information on this limit.

Input can follow output directly only with an intervening call to fflush or to a file-positioning function (fseek, fsetpos, or rewind). Output can follow input without an intervening call to a file-positioning function if the input operation encounters the end of the file.

Input and Output
Run-Time Routines by Category

Show: