重定向流 [英] redirecting streams
问题描述
我已经写了很多使用cout的程序,但现在我想把它输出到
一个文件而不是屏幕。我该怎么做?是平台特定的(如果
那么对不起)
谢谢
Mike
I''ve written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)
Thanks
Mike
推荐答案
迈克尔写道:
我写了很多关于使用cout的程序,但现在我想把这个
输出到一个文件而不是屏幕。我该怎么做?是平台特定的
(如果是那么抱歉)
I''ve written my progrma to use cout a lot, but now i want to output this
to a file instead of the screen. How do i do this Is it platform specific
(if so then sorry)
< off-topic>
从技术上讲,我相信'当你运行你的程序时,命令shell确定你正在使用
。但我认为我使用的每个命令行界面都是以相同的方式完成的:
myprog> outputfilename
或者,如果你想附加到文件:
myprog>> outputfilename
如果这不适合你,或者你没有从
命令行界面运行程序(也许你' 点击IDE上的一个按钮,你就可以在一个专门讨论你正在使用的b / b $ b $环境的新闻组上找到更好的运气。 />
< / off-topic>
-
Russell Hanneken
rg ******** @ pobox.com
删除''g''我的地址给我发邮件。
<off-topic>
Technically, I believe that''s determined by the command shell you''re using
when you run your program. But I think every command line interface I''ve
used has done it the same way:
myprog > outputfilename
Or, if you want to append to the file:
myprog >> outputfilename
If that doesn''t work for you, or if you''re not running the program from a
command line interface (maybe you''re clicking a button on an IDE), you''ll
probably have better luck asking on a newsgroup devoted to whatever
environment you''re using.
</off-topic>
--
Russell Hanneken
rg********@pobox.com
Remove the ''g'' from my address to send me mail.
Michael写道:
Michael wrote:
我写了很多关于使用cout的编程,但现在我想要将其输出到文件而不是屏幕。我该怎么做它是特定于平台的(如果
那么抱歉)
I''ve written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)
只需使用std :: ofstream(来自< fstream>)代替std :: cout。
Just use a std::ofstream (from <fstream>) instead of std::cout.
2004年5月21日星期五17:15:39 +0000(UTC),Michael
< SL *********** @ hotmail.com>写道:
On Fri, 21 May 2004 17:15:39 +0000 (UTC), "Michael"
<sl***********@hotmail.com> wrote:
我已经编写了我的程序来使用cout很多,但现在我想把它输出到
文件而不是屏幕。我该怎么做?是平台特定的(如果
那么抱歉)
谢谢
Mike
I''ve written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)
Thanks
Mike
如果你是尝试从命令行执行此操作而不更改
程序,请参阅Russell的回答。
如果要修改程序以进行推广,这是一个实施杰夫技术的实例的实例[并不是我认为他的答案不够,
它只是我已经写下这个,这样就不会去
浪费;-)]。请注意,所有实际输出都是由write_data(),
执行的,其概括方式与非成员运算符<<()的
非常相似按惯例编写(以ostream&作为参数):
//
//推广I / O
//
#include< iostream>
#include< fstream>
#include< cctype>
#include< cstdlib>
使用命名空间std;
void write_data(ostream& os);
int main()
{
cout<< 输出到屏幕或文件(f)? " ;;
char dest;
cin>> dest;
if((dest = std :: tolower(dest))=='s'')
write_data(cout);
else if(dest ==''f'')
{
ofstream(" gio_out.txt");
if(!of)
{
cout<< 无法创建gio_out.txt。 Bailing out。\ n";
退出(1);
}
write_data(of);
}
其他
{
cout<< 糟糕的输入。重新运行"。 <<结束;
退出(1);
}
返回0;
}
void write_data(ostream& os)
{
os<< 这是第1行\ n;
os<< 这是第2行\ n;
os<< etc.\ n;
}
-leor
-
Leor Zolman - - BD软件--- www.bdsoft.com
开 - C / C ++,Java,Perl和Unix中的现场培训
C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html
这篇关于重定向流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!