C and c++ output redirection method

  • 2020-04-01 21:37:48
  • OfStack

C:


#include<stdio.h>
int main(int argc,char* argv[])
{
    char test[]="c Language output redirection test ";
    int i;
    if (freopen("F:\ Essay a \test.txt", "w", stdout)==NULL)
        fprintf(stderr, " Redirection error! Unable to output to text n");
    for(i=0;i<=9;i++)
    {
        printf("%sn%dn",test,i);
    }
    fclose(stdout); 
    return 0;
}

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013314103346522.jpg" >

 

You can see above that escape characters can be written to a file.

C + + :


#include<fstream>  
#include <iostream>
using namespace std;
int main()
{   
  ofstream log("F:\ Essay a \test2.txt");
  streambuf * oldbuf =  cout.rdbuf(log.rdbuf());  
   cout << "c Language output redirection test "<<endl<<"123456" ;
  return 0;
}

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013314103516134.jpg" >

Related articles: