BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #80338同步于 2014/6/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

windows平台下,脚本调用C++程序,运行结果输出到文件中?

nicelee
2014/6/20镜像同步4 回复
例如 max.h #include <iostream> static void max(int &a, int &b) { int max = (a>b? a: b); std::cout << max << std::endl; } main.cpp #include <iostream> #include <cstring> #include "max.h" int main(int argc, char *argv[]) { if(strcmp(argv[1], "max") == 0) { int a = atoi(argv[2]); int b = atoi(argv[3]); max(a, b); } return 0; } 请教小伙伴们,windows平台下有办法可以输入参数到上面的main程序,然后输出结果定向到max.dat文件中吗? 在linux下比较方便,直接./max 1 2 > max.dat就可以,windows下呢?Python可以做到吗,具体代码怎么实现?
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
q397273499机器人#1 · 2014/6/20
一样的啊 通过『我邮2.0』发布
nicelee机器人#2 · 2014/6/20
解决了,太好了,thx 【 在 q397273499 的大作中提到: 】 : 一样的啊 : 通过『我邮2.0』发布
buptxrc机器人#3 · 2014/6/20
FILE *freopen(const char *path, const char *mode, FILE *stream); freopen("out.txt", "w", stdout); 应该可以.......
buptxrc机器人#4 · 2014/6/20
***@ubuntu:~/code/test$ cat freopen.cpp #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main() { freopen("out.txt", "w", stdout); cout << "hello" << endl; fclose(stdout); return 0; } ***@ubuntu:~/code/test$ g++ freopen.cpp && ./a.out ***@ubuntu:~/code/test$ cat out.txt hello 【 在 YouXia 的大作中提到: 】 : 这样是不可以的。 : linux下使用dup2和dup。 :