Thread: Problem with strings and filenames

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Problem with strings and filenames

    Hi,

    I have made this a simple command-line program which reads through a file and changes a few characters (ie, all "9" are replaces with "8"). It takes only a single argument, which is the input file. Now I want to make the output to be preceeded by a "_".

    This is the code I came up with after reading about some string functions:

    Code:
    char * filename = malloc( strlen(argv[1]) + 1);
    strcpy(filename, "_");
    strcat(filename, argv[1]);
    
    FILE *fp = fopen(filename, "wb");    /* Creates new file */
    It works when calling "my_app my_file.txt" from the console, however when I drag and drop a file from Windows, the argument includes the full path, so I get something like argv[1] = "C:\my_file.txt". When the "_" character is appended, the filename string is "_C:\my_file.txt", which crashes fopen, instead of "C:\_my_file.txt".

    Can someone give a practical example how to deal with this? I'm not sure how to deal with this string modification.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are going to add one letter to the string, you need to allocate 2 bytes longer than the original string, as one byte is consumed by the zero-termination, and one byte used up by the new character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use one of the string finder things to look for the last \ in your filename (hint: it's strrchr). That's where you want to put the _.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *AppendNewFilename(const char *oldFilename, char pattern)
    {
    	if(!oldFilename)
    	{
    		fprintf(stderr, "Invalid parameter for filename:{NULL}.\n");
    		return NULL;
    	}
    	else
    	{
    		unsigned int nFileLen = strlen(oldFilename)+2;
    		/* Allocate 2 more bytes one for '\0' and one for the pattern. */
    		char *generatedFilename = calloc(nFileLen, sizeof(char));
    		if(!generatedFilename)
    		{
    			fprintf(stderr, "Memory Allocation Error.\n");
    			return NULL;
    		}
    		else
    		{
    			unsigned int index, jndex;
    			for(index = 0, jndex = 0; index < nFileLen-1; index++)
    			{
    				if(index == 3)
    				{
    					generatedFilename[index] = pattern;
    					continue;
    				}
    				generatedFilename[index] = oldFilename[jndex++];
    			}
    			if(index == nFileLen-1)
    			{
    				generatedFilename[index] = '\0';
    				return generatedFilename;
    			}
    			else
    			{
    				fprintf(stderr,"For Loop Error.\n");
    				return NULL;
    			}
    		}
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	char *testfile = AppendNewFilename("C:\\my_file.txt", '_');
    	puts(testfile);
    	free(testfile);
    	printf("Hit enter to continue...\n");
    	getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  3. create an array of strings that hold filenames
    By finkus in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2005, 09:19 PM
  4. Getting filenames from a file
    By vinsta18 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2005, 11:52 AM
  5. Dynamic filenames?
    By Callith in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2004, 02:14 PM