Home > Mobile >  how to store results of ./a.out to a text file in c ?
how to store results of ./a.out to a text file in c ?

Time:12-06

I am wondering if there is any way to store the results of my program into a text file. this is my program :


void
search(unsigned int k, mybit t, mybits &e, mybits &r) {
  if (k == e.size()) {
    r.push_back(t);
    return;
  }
  if (t & e[k]) {
    search(k   1, t, e, r);
  } else {
    mybit v = e[k];
    while (v) {
      mybit t2 = v & -v;
      if (check_minimal2(t | t2, k   1, e)) {
        search(k   1, t | t2, e, r);
      }
      v = v ^ t2;
    }
  }
}
//------------------------------------------------------------------------
int
main(int argc, char **argv) {
  if (argc < 2) {
    std::cout << "usage: ./a.out inputfile "<< std::endl;
    return 0;
  }
  std::string filename = argv[1];
  mybits e;
  
  e = load_datfile(filename);
 
  mybits r;
  search(0, 0, e, r);
  
}

after I type:

make !./a.out input2.dat

My algorithm is performed and the results are out.txt:

usage: ./a.out <inputfile> out.txt 

CodePudding user response:

You could just use stdout and redirect your program output to a file.

In your code use

std::cout << "Whatever you want to end up in you output file";

And then call it with

./a.out <input-file> > out.txt

Since stdout ends up in you file, you should probably use stderr for you error message:

  if (argc < 2) {
    std::cerr << "usage: ./a.out inputfile "<< std::endl;
    return 0;
  }

CodePudding user response:

Your code already has a load_datfile() that you don't show here. In your exercise, you are supposed to write a store_datfile() function that writes a file similar how the inputfile is read. You then pass r as a const reference parameter to that function after calling search.

We don't know what the mybits type is and we are not supposed to write code for you.

  •  Tags:  
  • c
  • Related