Problems with strcpy/strcat

Posted on 2006-06-05
Last Modified: 2012-05-05
If I have the following code -

char *testfile; <--protected w/in TestParser class -

void TestParser::genFilenames()

  char* directory = getenv("TEST_ENV");

 char buffer[256];
 char bufferTime[80];
 time_t current;
 struct tm* now;
 char format[] = "%m%d%Y_%H%M%S";
 now = localtime(&current);
 strftime(bufferTime, sizeof(bufferTime), format, now);
              strcpy(buffer, directory);
      strcat(buffer, "test_");
      strcat(buffer, bufferTime);
                testfile = buffer;
void TestParser::logFiles()

What is the difference between using strcpy/strcat and sprintf?

I call genFilenames then logFiles.  logFiles does not output the filename.  It prints out garbage.
However, when I replace the strcpy and strcat lines with sprintf:

      sprintf(buffer1, "%s/test_%s.log", directory, bufferTime);

 the filename is displayed correctly.  Why is this the case?
Question by:jewee
    LVL 23

    Accepted Solution

    Your use of strcpy() and strcat() closely approximates your use of sprintf() [except for the / between directory and test_].

    Your problem is that once you return from genFilenames(), testfile still points to its local variable buffer[] that has gone out of scope.  

    Author Comment

    Thank you for the explanation - Why wouldn't it fail in both cases?
    LVL 30

    Expert Comment

    Hi jewee,
    > Why wouldn't it fail in both cases?

    Where is buffer1 declared at.  If buffer1 is outside of the function scope, then it will still exist when the function exits.


    Author Comment

    actually, that was a typo - buffer1 is buffer.
         sprintf(buffer, "%s/test_%s.log", directory, bufferTime);
    LVL 30

    Assisted Solution

    Then there should be no difference, but since you're returning a buffer that no longer exist, you're going to have undefined behavior, which can lead to inconsistent results.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now