07-18-2005, 10:52 AM | #1 (permalink) |
Crazy
|
[C] Incremental string updates - 00001.png 00002.png etc
Hi,
I am writing a program in C that will be outputting a lot of files that I want to be named 00001.png 00002.png 00003.png and so on. The way I am doing it at the moment is as follows: Code:
for(i=0; i < 10000; i++) { filename[4]++; if(filename[4] > 57) { filename[4] = 48; filename[3]++; if(filename[3] > 57) { filename[3] = 48; filename[2]++; if(filename[2] > 57) { filename[2] = 48; filename[1]++; if(filename[1] > 57) { filename[1]++; } } } } //CODE THAT WRITES TO FILE USING filename } Any ideas? Thanks Robbie Last edited by Jakejake; 07-18-2005 at 10:52 AM.. Reason: typo |
07-18-2005, 12:18 PM | #2 (permalink) |
"Officer, I was in fear for my life"
Location: Oklahoma City
|
You could do something like this:
Code:
string:filename; for i=1 to 10000 { filename=IntToStr(i)+.PNG; while length(filename) < 12 filename='0'+filename; //write file here } |
07-18-2005, 12:27 PM | #3 (permalink) |
Free Mars!
Location: I dunno, there's white people around me saying "eh" all the time
|
Never mind, was thinking in Java, not C
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war |
07-18-2005, 12:43 PM | #5 (permalink) |
Upright
Location: Chester, UK
|
My C is a bit rusty but sprintf is your friend here.
Try: Code:
char buffer[10]; for(int i=0; i<10000; i++) { sprintf(buffer, "%05d.png", i); /* write file contents */ } I dont have a C compiler anymore so I cant test this but it should be right, google will find you literally hundreds of guides to format strings with printf and sprintf though. Hope this helps |
Tags |
incremental, png, string, updates |
|
|