0

I've tried Sink and SaveHistory to do this but none worked. I've also tried things such as the code from this question from one of the answers. How to output to the console in C++/Windows. I made everything in an empty solution in Visual Studio 2019.

There weren't any errors but when I did:

    ofstream myfile;
    myfile.open("Addresses.txt");
    myfile << printf << endl;
    myfile.close();
    return 0;

it printed out random numbers.

//Code I tried
#include <iostream>

using namespace std;

int main (int) {
    cout << "This will print to the console!" << endl; // I don't know how to make it read all of the output
}

My goal is to put the whole output of the console in the text file I selected. (Addresses.txt)

elnineo
  • 3
  • 5
DGAF
  • 1
  • 1
  • Unrelated: `myfile << printf << endl;` is high risk of causing people to freak out over the re-use of `printf`. Unless you really are trying to print the `printf` function. In that case, Why? – user4581301 Sep 26 '19 at 03:18
  • 2
    Might be simpler to leave C++ behind and use a redirect at the command line: `MyProgram > myfile.txt` – user4581301 Sep 26 '19 at 03:20

1 Answers1

0

When you call myfile << printf << endl you aren't getting the text from the console, you are getting an identifier to the function. To get text from the console look at std::cin ex.

int x;
std::cin >> x;
std::cout << "You Entered: " << x << std::endl;

Sidenote, please avoid usage of using namespace this makes code much easier to read since it makes it clear if it is your function, or another libraries'