First off, your getFileName() function looks mighty complicated for simply returning a hardcoded filename. To simplify, it'd be better to use a #define. EG
>#define INPUT_FILE "myfile.txt"
then
>fopen(INPUT_FILE, "r");
(Oh, I see, just read your code a bit better, you want a number in the filename... )
To point out the problems with your code:
>char *temp2 = "";
>sprintf(temp2, "%d", in);
Here temp2 is defined as a pointer to a const string that is zero in length. So when you do the sprintf, the length of memory you have to play with is only one byte. Also, because it's defined as a string const (""), it will be placed in read only memory on some OS's.
The same applies for *temp1.
Also, you are returning a pointer to a section of memory that will go out of scope as soon as you leave the function.
Here's an improved version:
Code:
#include <stdio.h>
#include <string.h>
#define MAXL_FILENAME 256
#define FILENAME "input"
#define FILENAME_EXT ".IWP"
void getFileName(char *buf, int in)
{
sprintf(buf, "%s%d%s", FILENAME, in, FILENAME_EXT);
}
int main(void)
{
FILE *data_file, *current_file, *temp_file;
int data, current;
char current_name[MAXL_FILENAME], data_name[MAXL_FILENAME];
for (data = 0; data < 99; data++)
{
current = data + 1;
getFileName(current_name, current);
printf ("name is %s\n", current_name);
}
return 0;
}
I left the function in there, but it's only one line long, so it'd probably be better to forget the function, and put the sprintf() in the loop in main().
Hope this helps....