I just wanted to check whether my reasoning with this is correct. We use fopen to start a new file once we start the beginning of a new jpeg, and use the "w" (write) mode, right? And then we need to make sure that the "filename" argument to fopen adapts as we go through more and more jpegs on the card. The pset video recommended that we use sprintf for filenames, so are we meant to combine the two functions together? As in, somehow put the sprintf so that it's output is an argument to fopen Is this even possible, because I'm having some trouble?
1 Answer
When you open (and create) a file, you do like so:
fopen(name, mode);
where name
and mode
are string
s.
sprintf
is exactly like printf
, but instead of writing the formatted string
you specified to the standard output, it writes it to a string
variable (hint: like name
for example).
You do it like this:
char name[len];
sprintf(name, format as in printf, variable used in format);
fclose(lastfile);
fopen(name, mode);
Then each time you find a new beginning you create a new file with the relevant name. Don't forget to close the previous file first though!
If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!
-
Thanks. Not exactly what my problem was but turns out I overcomplicated everything in my head and I think I'm now sorted :) Commented Aug 29, 2015 at 19:22