Thread: alternative to strcat/strncat

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    13

    alternative to strcat/strncat

    hey guys,

    i want to use a string which includes a path to a folder several times for linking to different files that shall be copied. The path might change from machine to machine and the user is able to set it.


    at the moment I am using

    Code:
    CopyFileA(strcat(path,"file1"),.....);
    CopyFileA(strcat(path,"file2"),.....);
    
    and so on
    which of course does not work because
    Code:
     strcat
    overwrites path after each call - so for
    Code:
     file2
    it is not useable any more.

    is there a more comfortable way instead of e.g. defining a char variable for each file and copying the "initial" path once in it?

    Thanks for your answers...

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You could use sprintf function. Something like

    Code:
    char filepath[ BUIFSIZ ];
    
    sprintf( filepath, "%s%s%s%s", file1, file2, file3, file4 );
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would suggest you stop trying to compress your code into as few lines as possible.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    From what you are saying my understanding is that there is a "common" part to your path that you want to reuse?

    Using multiple variables like you suggested can certainly work.

    On another note, C does not have utilities for pretty much anything, it follows a do it yourself approach which increases the learning curve but at the same time gets you some very needed programming experience. In my experience, people who know C generally have an easier time adapting to other languages because they are not easily intimidated by not finding a function or method that solves their problem. They are used to building everything manually.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What i would probably say is to construct the filepath inside the function CopyFileA. This way you could avoid calling strcat many times and the concatenation will be done inside the function just once and use that path. Rather than constructing it at the call point.

    Ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-30-2007, 09:46 AM
  2. gotoxy alternative?
    By krappykoder in forum C Programming
    Replies: 7
    Last Post: 12-09-2005, 07:36 AM
  3. ASP.NET alternative to gallery.menalto.com ???
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 05-15-2005, 11:31 AM
  4. An alternative to encryption for game files?
    By blackwyvern in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-27-2002, 01:31 PM
  5. alternative o/s programming
    By iain in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-19-2002, 09:14 AM