Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 */ ");

}
Posted
Updated 22-Jun-18 5:12am
v2

1 solution

C++
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);
}
 
Share this answer
 
Comments
neveen neveen 22-Jun-18 12:43pm    
thank, another question please, how can use specific path in fp=fopen(filename,"w+"); I'm use debian.
neveen neveen 22-Jun-18 14:03pm    
I'm using something like this but the file is not created fp=fopen("home/pcdebian/folder/filename","w+");
Richard MacCutchan 22-Jun-18 15:38pm    
You need to add the path into the filename string at the sprintf command. You cannot put a variable name into a string and expect the system to understand it.
neveen neveen 22-Jun-18 16:21pm    
I do this, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900