your allocating enough room for modelname, inc, Lightcurves\\ and '/0'Code:temp = malloc(strlen(modelname) + strlen(inc) + strlen("Lightcurves\\") + 1); sprintf(temp, "%s%s%s%s", "Lightcurves\\", modelname, inc, ".mag");
then putting Lightcurves\\, modelname, inc and ".mag" in it.
there needs to be room for ".mag"......but really since it looks like the size of temp is the same each
iteration of the loop, you should just declare it statically, instead of allocating and freeing each time each time.
p.s. don't forget to close out somewhere