I'm writing software to rip data from a camera we designed, I need to take multiple images to manipulate in matlab. I am currently using fprintf to write files which is working fine.
But what I want to do is take 100-1000 frames and write each image to a file, incrementing the name of the file for every image.
Obviously a for loop would be ideal, but how do I have a variable filename in the fprintf statement.
fprintf takes FILE pointer as an argument. That means that you will have to close previous file and than open a new one with fopen. fopen will taka a file name which a string, and can be constructed using sprintf.
I would use sprintf and the loop index (appended to the filename), something like:
Code:
#define MAX 100
#define BASE_FILE "myfile_"
char filename[255];
int i;
FILE *f;
for (i=0;i<MAX;i++) {
sprintf(filename,"%s%03d",BASE_FILE,i);
f = fopen(filename);
if (f) {
// write data
fclose(f);
}
}