Output on Command prompt to a file
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
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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;
}
};
Labdabeta
Practically a Master Poster
615 posts since Feb 2011
Reputation Points: 27
Solved Threads: 31
Skill Endorsements: 1
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?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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;
}
Labdabeta
Practically a Master Poster
615 posts since Feb 2011
Reputation Points: 27
Solved Threads: 31
Skill Endorsements: 1
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?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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
triumphost
Practically a Master Poster
625 posts since Oct 2009
Reputation Points: 59
Solved Threads: 55
Skill Endorsements: 1
dont get that bit triumphost, wherebouts does that bit go in?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
Any replies when people are free would be great, thanks
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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.
triumphost
Practically a Master Poster
625 posts since Oct 2009
Reputation Points: 59
Solved Threads: 55
Skill Endorsements: 1
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.
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
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?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5