C ++文件重定向 [英] C++ file-redirection

查看:347
本文介绍了C ++文件重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于更快的输入,我看到你可以做文件重定向,并包括一个文件与 cin 输入已经设置。

For faster input, I read that you can do file-redirection and include a file with the cin inputs already set.

它的理论应该像下面一样使用

It theory it should be used like following

App.exe inputfile outputfile



根据我在C ++入门书中的理解,以下C ++代码[ 1]应该从文本文件中读取 cin 输入,并且不需要任何其他特殊指示,如[2]

As far as I understood in the C++ Primer book, The following C++ code[1] should be reading cin input from the text file and shouldn't need to any other special indication like[2]

[2]

include <fstream>
ofstream myfile;
myfile.open ();

[1]以下C ++代码...

[1] The following C++ code...

#include <iostream>
int main(){
    int val;
    std::cin >> val; //this value should be read automatically for inputfile
    std::cout << val;
    return 0;
}

我错过了什么?

推荐答案

要使用你的代码[1],你必须像这样调用你的程序:

To use your code [1] you have to call your program like this:

App.exe < inputfile > outputfile

您也可以使用:

App.exe < inputfile >> outputfile

在这种情况下,输出不会在每次运行命令时重写,

In this case the output wouldn't be rewrited with every run of the command, but output will be appended to already existing file.

有关在Windows中重定向输入和输出的详细信息,您可以找到在这里

More information about redirecting input and output in Windows you can find here.

请注意,< > > ; 符号将被输入 —他们不仅仅是为了演示的目的在这个解释。因此,例如:

Note that the <, > and >> symbols are to be entered verbatim — they are not just for presentation purposes in this explanation. So, for example:

App.exe < file1 >> file2

这篇关于C ++文件重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆