Jeugasce
Jeugasce

Reputation: 197

printing system command to text file c++

I have the following code, but when I run it, the output is printed instead of being copied to the text file.

void checkText()
{
    ifstream my_file("test.txt");
    if (my_file.good()) {
        cout << "File exist" ;
    }
    else {
        ofstream outputFile;
        outputFile.open("test.txt");
        outputFile << system("head -n 1024 words.txt");
        outputFile.close();
        cout << "Done!\n";
    }
}

How do I print the system command to my text file?

Upvotes: 1

Views: 1464

Answers (2)

user6904350
user6904350

Reputation:

you can also use for printing the output in a text file freopen(); command

freopen("input file name.txt", "r", stdin);
freopen("output file name.txt", "w", stdout);

Upvotes: 0

crashmstr
crashmstr

Reputation: 28573

system returns an int and not the "output" from the command that is executed.

One way to do this would be to redirect the command to the file you want:

    std::system("head -n 1024 words.txt > test.txt");

Upvotes: 1

Related Questions