Gudday all
Been fiddling around with parsing strings in a file into their path and file name components and finding that my ability to handle them is not as great as I thought. The basic problem is that a string that I wish to contain a file name of 13 characters (incl. the '\0') appears to be a lot longer that that when printed out.
I have a file called moveTiff.dat which contains lines of the form \TestingTiffMoves\AU\PICKLIST\xxxxxxxx.TIF where the path is \TestingTiffMoves\AU\PICKLIST\ and this will be consistent for all lines in the file. The file is to be moved to another location. My idea was to use extract the path and file information and then use rename() to move the files as appropriate.
I can extract the path seemingly alright but the file name has me stumped.
When I run the code generated i get an output shown in the attachment.Code:#include <stdio.h> #include <string.h> #include <io.h> int main(int argc, char *argv[]) { char dest[30] = {0}; //destination path of files to be moved char tiff_file[13] = {0}; //TIFF files that will be moved char move_tiff[13] = "moveTIFF.dat"; //file with list of TIFF files to be moved char tiffLine[43]; //name and path of TIFF file FILE *inputFile, *errorFile, *indexFile, *tiffFile; if ( (access(move_tiff, F_OK)) != -1 ) { printf("moveTIFF.dat found and can be opened\n"); tiffFile = fopen(move_tiff, "r"); // open data file "moveTIFF.dat" for reading only while (fgets(tiffLine, 43, tiffFile)!=NULL) { //loop for reading a line up of "moveTIFF.dat" to end of file printf("%s\n", tiffLine); strncpy(dest, tiffLine, 30); printf("%i\n", strlen(tiffLine)); strncpy(tiff_file, tiffLine + (strlen(tiffLine)-12), 12); printf("%s %s\n", dest, tiff_file); } } fclose(tiffFile); return 0; }
I can find the file moveTIFF.dat and open it. The lines in the file can be printed out (I have only shown the output for a run on a single line but the result is the same for all lines).
The length of the line, 42 chars without the '\0' seems to be correct . I can print out the path seemingly ok but the file name is wrong. It is the extra lines (4 of them) and the other characters (the single '1' and the group of 3 characters) that puzzle me.
The extra lines and characters appears in all lines of the file when run. I thought that I had made sure that only the characters I require were being extracted but somewhere it went blah.
I am sure the problem is obvious but I cannot see the wood for the trees at present. Hopefully someone can spot what I cannot.