"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.hku.hk...
Thanks much for all your suggestion. And i won't multipost anymore. Just
another question. As printf is for output to screen, what if i want to
output the result into a text file.
i've written:
__________________________________
ofstream of ("abc.txt", ios::out);
if (!ofs){
cerr << "Error" << endl;
return 1;
}
display_number(12345678); // this just output to screen. But how to change
the function "display_number" to output the result to abc.txt???or is
there any simpler ways ????
This is a simple way, this is the only way in C++. You need to pass the
destination you want to display to as a parameter to display_number and
_display_number, that means you are going to have to rewrite _display_number
to use C++ I/O instead of printf.
void _display_number(ostream& os, int v, int n){
...
}
void display_number(ostream& os, int v){
_display_number(os, v < 0 ? -v : v,v < 0);
}
ofstream ofs("abc.txt", ios::out);
if (!ofs){
cerr << "Error" << endl;
return 1;
}
display_number(ofs, 12345678);
Now I think you should have a go at rewriting _display_number yourself.
Instead of cout << ..., you use the parameter passed in like this, os << ...
john