Thread: Using variables as place markers for strings?

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Using variables as place markers for strings?

    (Sorry if the title does not really match the question.)

    I have been hardcoding filenames into my code but have been told that that is error prone and hard to maintain.
    I have tried to recode to achieve this but am getting complier errors.
    As an example I have a file name declared and defined as shown below.
    Code:
    char index_file[10] = "index.dat"; //correctly formatted files stored in here
    Further on in my code I have need to use that file name. At present I need to hard code it in e.g.
    Code:
    strncat(shorttiffUrl, "index.dat", 40);
    Is it possible to replace the index.dat by something that points to the original declaration?
    I have tried
    Code:
    strncat(shorttiffUrl, index_file, 40);
    and
    Code:
    strncat(shorttiffUrl, *index_file, 40);
    but both give complier errors e.g
    Code:
    C:/ScanImages/findRPTfiles-4.cpp: In function `int main(int, char**)':
    C:/ScanImages/findRPTfiles-4.cpp:204: error: invalid conversion from `char' to `const char*'
    C:/ScanImages/findRPTfiles-4.cpp:204: error:   initializing argument 2 of `char* strncat(char*, const char*, size_t)'
    mingw32-make.exe[1]: *** [Debug/findRPTfiles-4.o] Error 1
    I am sure that the answer is staring me in the face but I cannot see it.
    I know that this will be useful for later in my nascent coding career.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Should work fine. Can you post a small, compilable program that can reproduce the error?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    More specifically, the first one should work fine and the second one should die a horrible death.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, both strncpy and strncat are broken, IMO. The first isn't guaranteed to append a null terminator, and the latter adds the length of the destination string to the length parameter to obtain the maximum total size of the string. Both of these could lead to some nasty bugs. Here are a couple of 'safe' replacements:

    Code:
    char* sstrncpy( char* dst, char const* src, size_t max )
    {
    	char* 
    		ptr = dst;
    	if( max != 0 )
    	{
    		while( --max && *src )
    			*ptr++ = *src++;
    		*ptr = 0;	
    	}
    	return dst;
    }
    
    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;
    }

  5. #5
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Et al
    I found an error earlier in the code. Having a lower case letter rather than an upper case can pose problems.

    Thank you for your replies.

  6. #6
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by Sebastiani View Post
    Also, both strncpy and strncat are broken, IMO. The first isn't guaranteed to append a null terminator, and the latter adds the length of the destination string to the length parameter to obtain the maximum total size of the string. Both of these could lead to some nasty bugs. Here are a couple of 'safe' replacements:

    Code:
    char* sstrncpy( char* dst, char const* src, size_t max )
    {
    	char* 
    		ptr = dst;
    	if( max != 0 )
    	{
    		while( --max && *src )
    			*ptr++ = *src++;
    		*ptr = 0;	
    	}
    	return dst;
    }
    
    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;
    }
    Sebastiani
    In an earlier thread you showed me the sstrncpy and I have used it in my code. it works well. I will try the sstrncat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. Having trouble correctly setting DJGPP's env. variables in XP
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-05-2002, 10:51 PM