9

I'm trying to write console data into a separate text file in cpp. Anybody help me with sample code.

3
  • 1
    console data of what process? your own process? some other process you create? Commented Jul 17, 2010 at 9:17
  • What is "console data"? How does your current approach look like? Commented Jul 17, 2010 at 9:19
  • 2
    Anybody who search for an answer look for user11977's answer. It is the correct one. Commented Sep 5, 2015 at 15:55

6 Answers 6

20

There are various ways to do this. You could redirect it from the command line with programname > out.txt. Or you could use freopen("out.txt","w",stdout); at the start of your program.

Sign up to request clarification or add additional context in comments.

1 Comment

This was working well for me. But suddenly the cmd console outputs were completely invisible. When I remove this line, cmd outputs were visible back
3

If you want to write from your own process, I'd suggest a simple print method

void print(const string str, ostream & output)
{
    output << str;
}

Then you can call

print("Print this", cout);

for console output, or

ofstream filestream("filename.out");
print("Print this", filestream);

to write into a file "filename.out". Of course you gain most, if print is a class method that outputs all the object's specific information you need and this way you can direct the output easily to different streams.

1 Comment

The print function you defined has almost no benefit. You can just as easily write cout << "Print this"; for you first example and filestream << "Print this";" in the second example. Unless print` add some extra logic (perhaps writing a timestamp or perhaps writing to standard output and a file), there is no benefit to such a function.
0

If you want to create a child process and redirect its output you could do something like this:

FILE* filePtr = popen("mycmd");
FILE* outputPtr = fopen("myfile.txt");

if(filePtr && outputPtr) {
    char tmp;
    while((tmp = getc(filePtr)) != EOF)
        putc(tmp, outputPtr);

    pclose(filePtr);
    fclose(outputPtr);
}

Comments

0

bbtrb wrote:

void print(const string str, ostream & output) { output << str; }

Better than this is of course

ostream& output(ostream& out, string str) {out << str; return out;}

so that you can even have the manipulated output stream returned by the function.

Comments

0

smerrimans answer should help you out.

There is also the option to implement your own streambuf and use it with std::cout and std::cerr to store printouts to file instead of printing to console. I did that a while ago to redirect printouts to some sort of rotating logs with timestamps.

You will need to read up a little bit on how it works and this book helped me get it right.

If that's not what you're after it is a bit of overkill though.

Comments

0

Create file -> redirect input into file

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string file_name;
    std::string str;
  
    std::cout << "Add file name:";
    std::cin >> file_name;

    std::cout << "Open file:" << file_name << std::endl;

    std::ofstream oFile(file_name);

    if (!oFile) {
        std::cout << "ERROR: we can't open file:" << file_name << std::endl;
        return 1;
    }
    else {
        std::cout << "File is open: " << oFile.is_open() << std::endl;
    }

    std::cout << "Enter you sentence into file:\n";
    getline(std::cin >> std::ws, str);
    oFile << str;
    oFile.close();


    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.