流的重定向 [英] Redirection of Streams
问题描述
我使用C很长一段时间我可以打开文件进行输入但是如果
文件不存在,那么我将输入重定向到标准输入,
类似于:
FILE * id = fopen(" data.in"," r");
if(!id)
id = stdin;
我读过关于溪流但我还没找到怎么做,我想是/>
它必须如下所示:
fstream id(" data.in",ios :: in)
if(!id )
//这里必须写什么,所以id stream是connected to cin?
Gaijinco写道:我用C很长一段时间我可以打开一个文件进行输入,但是如果
文件不存在,那么我将输入重定向到标准输入,
类似于:
> FILE * id = fopen(" data.in"," r");
if(!id)
id = stdin;
我我已经读过关于溪流但我还没找到怎么做,我想它必须看起来像:
fstream id(" data.in",ios :: in)
if(!id)
//这里必须写什么,所以id stream是connected cin?
您实际上可以通过使用指针来流式传输对象,如下所示:
> >
std :: istream * in = new std :: ifstream(" data.in");
if(!(* in))
in =& std :: cin;
JH Trauntvein写道:Gaijinco写道:我使用C很长一段时间我可以打开一个文件进行输入,但如果
文件不存在,那么我将输入重定向到标准输入,
类似于:
FILE * id = fopen(data.in,r);
if(!id)
id = stdin;
<我读过关于溪流但我还没找到怎么做,我想它必须看起来像:
fstream id(" data.in",ios :: in)
if(!id)
//这里必须写什么,所以id stream是connected cin?
你实际上可以通过使用指针来流式传输对象,如下所示:
std :: istream * in = new std :: ifstream(" data.in");
if(!(* in))
in =& std :: cin;
>
或者,最好是对溪流的引用。
Ben Pope
-
我不是只是一个数字。对很多人来说,我被称为弦乐......
Ben Pope写道:
JH Trauntvein写道:< blockquote class =post_quotes> Gaijinco写道:我用C很长一段时间我可以打开一个文件进行输入但如果
文件不存在,那我就重定向了标准输入的输入,例如:
FILE * id = fopen(" data.in"," r");
如果(!id)
id = stdin;
我读过关于溪流但我还没有找到怎么做,我想它必须看起来像:
fstream id(" data.in",ios :: in)
if(!id)
//这里必须写什么,所以id stream是& ;连接" cin?
你实际上可以通过使用指针来流式传输对象,如下所示:
std :: istream * in = new std :: ifstream(" data.in");
if(!(* in))
in =& std :: cin;
>或者,最好是对流的引用。
如何使用引用而不是
指针来解决这个特定问题。使用指向流而不是
流的指针的全部意义在于能够使其指向不同的流,具体取决于某些测试的结果
。有参考,那不会飞:你不能
重新参考。
最好
Kai-Uwe Bux
I used C for a long time where I could open a file for input but if the
file didn''t existed, then I redirected the input to the standard input,
something like:
FILE *id=fopen("data.in","r");
if(!id)
id=stdin;
I have read about streams but I haven''t found how to do it, I suppose
it must look like:
fstream id("data.in",ios::in)
if(!id)
// what must be written here so id stream is "connected" to cin?
Gaijinco wrote:I used C for a long time where I could open a file for input but if the
file didn''t existed, then I redirected the input to the standard input,
something like:
FILE *id=fopen("data.in","r");
if(!id)
id=stdin;
I have read about streams but I haven''t found how to do it, I suppose
it must look like:
fstream id("data.in",ios::in)
if(!id)
// what must be written here so id stream is "connected" to cin?
You could actually do this by using pointers to stream objects as in
the following:
std::istream *in = new std::ifstream("data.in");
if(!(*in))
in = &std::cin;
JH Trauntvein wrote:Gaijinco wrote:I used C for a long time where I could open a file for input but if the
file didn''t existed, then I redirected the input to the standard input,
something like:
FILE *id=fopen("data.in","r");
if(!id)
id=stdin;
I have read about streams but I haven''t found how to do it, I suppose
it must look like:
fstream id("data.in",ios::in)
if(!id)
// what must be written here so id stream is "connected" to cin?
You could actually do this by using pointers to stream objects as in
the following:
std::istream *in = new std::ifstream("data.in");
if(!(*in))
in = &std::cin;
Or, preferably, references to streams.
Ben Pope
--
I''m not just a number. To many, I''m known as a string...
Ben Pope wrote:
JH Trauntvein wrote:Gaijinco wrote:I used C for a long time where I could open a file for input but if the
file didn''t existed, then I redirected the input to the standard input,
something like:
FILE *id=fopen("data.in","r");
if(!id)
id=stdin;
I have read about streams but I haven''t found how to do it, I suppose
it must look like:
fstream id("data.in",ios::in)
if(!id)
// what must be written here so id stream is "connected" to cin?
You could actually do this by using pointers to stream objects as in
the following:
std::istream *in = new std::ifstream("data.in");
if(!(*in))
in = &std::cin;
Or, preferably, references to streams.
How would you go about this particular problem using references instead of
pointers. The whole point of using a pointer to a stream rather than a
stream was to be able to make it point to a different stream depending on
the outcome of some test. With a reference, that won''t fly: you cannot
reseat a reference.
Best
Kai-Uwe Bux
这篇关于流的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!