Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何指定qt creator从何处读取数据_C++_Qt_Qt Creator - Fatal编程技术网

C++ 如何指定qt creator从何处读取数据

C++ 如何指定qt creator从何处读取数据,c++,qt,qt-creator,C++,Qt,Qt Creator,我希望qt creator从文件中读取数据,就像从std::cin读取数据一样。 这就是我想要的东西 cat in.txt type | ./may_executable_file 我需要它来测试qt creator中的程序,而不是每次都在控制台中收集相同的数据。您应该考虑将程序重新构造为可测试的方法,这些方法将从istream读取,而不是直接从cin读取。例如: int do_work(std::istream& is, std::ostream& os) { int

我希望qt creator从文件中读取数据,就像从
std::cin
读取数据一样。 这就是我想要的东西

cat in.txt type | ./may_executable_file

我需要它来测试qt creator中的程序,而不是每次都在控制台中收集相同的数据。

您应该考虑将程序重新构造为可测试的方法,这些方法将从
istream
读取,而不是直接从
cin
读取。例如:

int do_work(std::istream& is, std::ostream& os)
{
    int n, k;
    is >> n >> k;
    os << n << std::endl << k << std::endl;
}

int main()
{
   do_work(std::cin, std::cout);
   return 0;
}
另一个选项是在
QCreator
的运行设置中使用
自定义可执行文件

int test_work()
{
    std::ifstream inf("test.txt");
    std::stringstream out;
    do_work(inf, out);

    // Check out for results here.
}