如何关闭控制台输出,即之后的cout无效

老王爱上猫 2014-05-26 05:10:34
如题,求大神帮忙看下!
...全文
470 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-05-27
  • 打赏
  • 举报
回复
freopen, _wfreopen
Reassign a file pointer.

FILE *freopen( const char *path, const char *mode, FILE *stream );

FILE *_wfreopen( const wchar_t *path, const wchar_t *mode, FILE *stream );

Function Required Header Compatibility 
freopen <stdio.h> ANSI, Win 95, Win NT 
_wfreopen <stdio.h> or <wchar.h> Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

Each of these functions returns a pointer to the newly opened file. If an error occurs, the original file is closed and the function returns a NULL pointer value.

Parameters

path

Path of new file

mode

Type of access permitted

stream

Pointer to FILE structure

Remarks

The freopen function closes the file currently associated with stream and reassigns stream to the file specified by path. _wfreopen is a wide-character version of _freopen; the path and mode arguments to _wfreopen are wide-character strings. _wfreopen and _freopen behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_tfreopen freopen freopen _wfreopen 


freopen is typically used to redirect the pre-opened files stdin, stdout, and stderr to files specified by the user. The new file associated with stream is opened with mode, which is a character string specifying the type of access requested for the file, as follows:

"r"

Opens for reading. If the file does not exist or cannot be found, the freopen call fails.

"w"

Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it does not exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it does not exist.

Use the "w" and "w+" types with care, as they can destroy existing files.

When a file is opened with the "a" or "a+" access type, all write operations take place at the end of the file. Although the file pointer can be repositioned using fseek or rewind, the file pointer is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. In addition to the above values, one of the following characters may be included in the mode string to specify the translation mode for new lines.

t

Open in text (translated) mode; carriage return–linefeed (CR-LF) combinations are translated into single linefeed (LF) characters on input; LF characters are translated to CR-LF combinations on output. Also, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading or for writing and reading with "a+", the run-time library checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file may cause fseek to behave improperly near the end of the file. The t option is a Microsoft extension that should not be used where ANSI portability is desired.

b

Open in binary (untranslated) mode; the above translations are suppressed.

If t or b is not given in the mode string, the translation mode is defined by the default mode variable _fmode.

For a discussion of text and binary modes, see Text and Binary Mode File I/O.

Example

/* FREOPEN.C: This program reassigns stderr to the file
 * named FREOPEN.OUT and writes a line to that file.
 */

#include <stdio.h>
#include <stdlib.h>

FILE *stream;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}


Output

successfully reassigned
This will go to the file 'freopen.out'


Stream I/O Routines

See Also   fclose, _fdopen, _fileno, fopen, _open, _setmode
老王爱上猫 2014-05-27
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
reopen ?

//-插入某种操作
cou<<"111"<<endl; //次句不会显示在控制台上
lin5161678 2014-05-27
  • 打赏
  • 举报
回复
fclose(stdout);
赵4老师 2014-05-26
  • 打赏
  • 举报
回复
reopen ?
昵称很不好取 2014-05-26
  • 打赏
  • 举报
回复
linux下close(STDOUT_FILENO);就行了吧?

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧