freopen()重定向
freopen函数: 使用不同的文件或模式重新打开流,即重定向 C++ //把一个字符串写入到文件中去: #include<iostream> using namespace std; int main(void) { /* redirect standard output to a file */ if(freopen("D:\\out.txt", "w", stdout) == NULL) fprintf(stderr,"error redirecting stdout\n"); /* this output will go to a file */ printf("This will go into a file.\n"); string s = "12345"; cout<<s<<endl; int a,b; while(cin>>a>>b){ cout<<a<<b<<endl; } /*close the standard output stream*/ fclose(stdout); return 0; } View Code #include <iostream> using namespace std; int main(void) { FILE *fp; fp = freopen("D:\\in.txt","r",stdin); //freopen() 的返回值