Gudday all
Still trying to finish my 1st large (for me anyway) C program at work. Slwoly getting rid of the bugs and errors. Presently I have a situation where variable is being changing to a non-desired avlue and I can't see how, or why, it is being changed.
I am replacing hard-coded values ("index.dat") with variables so that I only need make changes in 1 place instead of many. The program works well with the variable until this one spot near the end. The enboldened portion is the code in question. The variable is called index_file. It has the correct value until the code reaches the function called sstrncpy when it changes yet the function does not need or use that variable.
The attachment shows the change in the variable's value as it progresses through the code. I cannot see why it changes from "index.dat" to "\"?
Maybe it is staring at me in the face but I certainly am oblivious to it. Perhaps I am referencing the variable incorrectly?
Code:
// a 'safe' strncpy
char* sstrncpy(char* dst, const char* src, size_t len)
{
printf("The source string is %s\n", src);
if (!len--)
return NULL;
strncpy(dst, src, len);
dst[len] = 0;
printf("inside sstrncpy: - The dest string is %s\n", dst);
return dst;
}
// a 'safe' strncat
char* sstrncat(char* dst, char const* app, size_t max )
{
char*
ptr = dst;
if( max != 0 )
{
while( --max && *ptr )
++ptr;
while( max-- && *app )
*ptr++ = *app++;
*ptr = 0;
}
return dst;
}
int main (int argc, char *argv[])
{
FILE *indexFile;
char index_file[10] = "index.dat"; //files that have errors in them stored in here
char *tiffUrl=""; //string contains full path incl. file name where TIFF file will end up
char shorttiffUrl[] ="";
int tiffLength = 12;
int 1;
tiffFile = fopen (move_tiff, "w+"); /* open data file "moveTIFF.dat" */
indexFile = fopen (index_file, "w+"); /* open data file "index.dat" */
.....
/* This portion will add index.dat to moveTIFF.dat. Needs to get path as well */
i = strlen(tiffUrl) - tiffLength + 1; // the + 1 is to grab the last '\' of the path
printf("1. index_file after opening is %s\n", index_file);
printf("shorttiffUrl is %s and tiffUrl is %s and i is %d\n", shorttiffUrl, tiffUrl, i);
printf("2. index_file after opening (and this is the line before sstrncpy) is %s\n", index_file);
sstrncpy(shorttiffUrl, tiffUrl, i);
printf("3. After sstrncpy ----- The dest string is %s\n", shorttiffUrl);
//printf("After sstrncpy ----- shorttiffUrl is %s and tiffUrl is %s\n", shorttiffUrl, tiffUrl);
printf("4. index_file before index.dat written to moveTiff.dat is %s\n", index_file);
sstrncat(shorttiffUrl, index_file, 40);
printf("After strncat ----- shorttiffUrl is %s\n", shorttiffUrl);
fprintf(tiffFile, "%s\n", shorttiffUrl);
/* */
.....
fclose(indexFile);
fclose(tiffFile);
...
}