neu*_*cer 1 c++ recursion file
我将有两个类功能.第一个类函数打开文件.然后它调用第二个函数,该函数写入文件并递归调用自身.当第二个函数完成时,原始函数将关闭文件.
是否有可能做到这一点?
当然,只要将文件句柄/对象传递给递归函数:
void recursion(int data, int maxdepth, ostream &os)
{
// must eventually break out
if (maxdepth == 0)
return;
// write data
os << data << std::endl;
// and call one deeper
recursion(data + 1, maxdepth - 1, os);
}
void start(const char *filename)
{
std::ofstream os(filename);
recursion(0, 100, os);
}
Run Code Online (Sandbox Code Playgroud)
是的,只要你的递归函数有一个基本情况,它就会终止.
func2(int p) {
if (p == 0) return;
//write
func2(--p);
}
func() {
//open file
func2(10);
//close file
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2933 次 |
最近记录: |