how do i get output which would normally appear on the screen, as in the Microsoft visual studio command prompt, written to a file such as a simple text file. say << X << is the output, what do i do from here? thanks

You could use an fstream object. Here is a nice wrapper class to act exactly like cout except it goes to screen and file:

class fcoutstream
{
    private:
    fstream file;
    public:
    fcoutstream(const char *fname){file.open(fname);}
    ~fcoutstream(){file.close();}
    template <typename T> fcoutstream &operator<<(const T& t)
    {
        cout<<t;
        file<<t;
        return *this;
    }
    template <typename T> fcoutstream &operator>>(T& t)
    {
        //you need to decide which one you want:
        //read from file:
        //file>>t;
        //read from cin:
        cin>>t;
        return *this;
    }
};

well here is my code here, just wondering if anyone can help

include <iostream>

include <cmath> // appropriate c++ libraries for functions & constants needed

using namespace std;

const int N = 1000;
const double pi = 3.14159265; //constants applied

int main()
{

double num, factor, avg = 1.0; //variables declared and initialised
int i;

num = N - 1;
factor = (N - 1) / N;

for(i=2;i<=N;i++) //loop initiated and calculation
{
avg = avg + factor;
factor = factor * (N - i) / N;
}

cout << "Approximately, the average number of random songs played before a repeat song is "
<< (sqrt(pi * N / 2.0) - 1.0 / 3.0 + sqrt(pi / (2.0 * N)) / 12.0 - 4.0 / (135.0 * N)) << endl; //output message on the command prompt and tabulated answer to the formula/equation

return 0;
}

also how do you do the code part, instead of pasting here?

also how do you do the code part, instead of pasting here?

This is an easy one just hit ENTER twice so that you have 2 blank lines. Now paste your code on the bottom one, then select all the code and hit TAB once.

As for file writing, once you have your answer you can use the fstream library like so:

#include <fstream>
using namespace std;
int main()
{
    cout<<"Testing file IO..."<<endl;
    fstream file("myFile.txt");//opens myFile.txt
    file<<"This is a test... 3*3="<<3*3<<endl;//file output
    file.close();
    return 0;
}

include <iostream>

#include <cmath>  // appropriate c++ libraries for functions & constants needed
#include <fstream>
using namespace std;

const int N = 1000;
const double pi = 3.14159265; //constants applied

int main()
{

double num, factor, avg = 1.0;   //variables declared and initialised
int i;

num = N - 1;
factor = (N - 1) / N;


for(i=2;i<=N;i++)  //loop initiated and calculation
{
avg = avg + factor;
factor = factor * (N - i) / N;
}


cout << "Approximately, the average number of random songs played before a repeat song is "
<< (sqrt(pi * N / 2.0) - 1.0 / 3.0 + sqrt(pi / (2.0 * N)) / 12.0 - 4.0 / (135.0 * N)) << endl; //output message on the command prompt and tabulated answer to the formula/equation
{
cout<<"Testing file."<<endl;
fstream file("myFile.txt");//opens myFile.txt
file<<"This is a test... 3*3="<<3*3<<endl;//file output
file.close();
return 0;
}
}

as well as the usual output printed on the command prompt, i want the output/answer printed to a created text file, is this happening in the above? if not where am i going wrong?

include <iostream>

#include <cmath>  // appropriate c++ libraries for functions & constants needed
#include <fstream>
using namespace std;

const int N = 1000;
const double pi = 3.14159265; //constants applied

int main()
{

double num, factor, avg = 1.0;   //variables declared and initialised
int i;

num = N - 1;
factor = (N - 1) / N;


for(i=2;i<=N;i++)  //loop initiated and calculation
{
avg = avg + factor;
factor = factor * (N - i) / N;
}


cout << "Approximately, the average number of random songs played before a repeat song is "
<< (sqrt(pi * N / 2.0) - 1.0 / 3.0 + sqrt(pi / (2.0 * N)) / 12.0 - 4.0 / (135.0 * N)) << endl; //output message on the command prompt and tabulated answer to the formula/equation
{
cout<<"Testing file."<<endl;
fstream file;
file.open ("MP3.txt");//opens MP3.txt
file<< (sqrt(pi * N / 2.0) - 1.0 / 3.0 + sqrt(pi / (2.0 * N)) / 12.0 - 4.0 / (135.0 * N)) <<endl;//file output
file.close();
return 0;
}
}

thought that was it there, put it doesnt produce the text file, any helpers?

FILE *stream ; //Redirect Output from console to a TextFile
if((stream = freopen("C:\Somefile.txt", "w", stdout)) == NULL)

stream = freopen("CON", "w", stdout); //Redirect Output to the Screen

dont get that bit triumphost, wherebouts does that bit go in?

Any replies when people are free would be great, thanks

That would go anywhere that you want stuff to print to and from the screen.

Example:

if you wanted to get everything written to the console into a file.. you can do:

FILE *stream ; //Redirect Output from console to a TextFile
if((stream = freopen("C:\Somefile.txt", "w", stdout)) == NULL)
{
    cout<<"Meh this will be written to a file.. and anything inbetween these braces will be written to C:\SomeFile.txt.. Even user input will get put into that file.";
}

stream = freopen("CON", "w", stdout); //Redirect Output to the Screen

cout<<"Now anything after that line above such as this line.. will be printed to the console screen..";

//Now if you close the stream, it will print to the screen automatically.

That's how I used it when Narue gave me it a couple years back when I needed to write the console to the file. Not sure if I used it right the first time I got it but that's how I'd still use it and it works.

Edited 4 Years Ago by triumphost

it doesnt open the text file,though. wanted to print the answer to come up on the command prompt screen but also open a text file, that the outputs printed on.

include <iostream>

#include <cmath>  // appropriate c++ libraries for functions & constants needed
#include <fstream>
#include <stdlib.h>

using namespace std;

const int N = 1000;
const double pi = 3.14159265; //constants applied

int main()
{

double num, factor, avg = 1.0;   //variables declared and initialised
int i;

num = N - 1;
factor = (N - 1) / N;


for(i=2;i<=N;i++)  //loop initiated and calculation
{
avg = avg + factor;
factor = factor * (N - i) / N;
}


cout << "Approximately, the average number of random songs played before a repeat song is "
<< (sqrt(pi * N / 2.0) - 1.0 / 3.0 + sqrt(pi / (2.0 * N)) / 12.0 - 4.0 / (135.0 * N)) << endl; //output message on the command prompt and tabulated answer to the formula/equation
{ 
ofstream out("Z:\mp3.txt"); 
if(!out) { 
cout << "Cannot open file.\n"; 
return 1; 
} 

out << "This is a short text file."; 
out.close(); 

system("wordpad.exe Z:\mp3.txt") ;
return 0; 
}

}

thought that would work, but to no avail, any ideas people?

I'm just not sure the following statement will work with a full path

ofstream out("Z:\mp3.txt"); 

Why don't you try the following...

ofstream out("mp3.txt");
This article has been dead for over six months. Start a new discussion instead.