如何在循环中写入时动态更改文件名? [英] How to dynamically change filename while writing in a loop?

查看:129
本文介绍了如何在循环中写入时动态更改文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件名发送到函数以在文件上写入一些数据,在第一次迭代中将一些内容写入名为file0.txt的文件,第二次迭代file1.txt等等,

该怎么办?



我的尝试:



I want to send the filename to function to write some data on file, in the first iteration write some content into a file named file0.txt, second iteration file1.txt and so on,
how can do that?

What I have tried:

void main(){
writetotxtfile(str,"file/*how can make this part dynamically change */ ");

}

推荐答案

char* base = "file";
char filename[256];
int index;
for (index = 0; index < uppervalue; index++)
{
    sprintf(filename, "%s%d.txt", base, index);
    writetotxtfile(filename, other parameters);
}


这篇关于如何在循环中写入时动态更改文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆