[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with strcpy/strcat

Posted on 2006-06-05
5
Medium Priority
?
669 Views
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";
 time(&current);
 now = localtime(&current);
 strftime(bufferTime, sizeof(bufferTime), format, now);
 
              strcpy(buffer, directory);
      strcat(buffer, "test_");
      strcat(buffer, bufferTime);
      strcat(buffer,".log");
                testfile = buffer;
}
void TestParser::logFiles()
{
  cout<<testfile<<endl;

}
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?
0
Comment
Question by:jewee
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 1800 total points
ID: 16837687
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.  
0
 

Author Comment

by:jewee
ID: 16837739
Thank you for the explanation - Why wouldn't it fail in both cases?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16838264
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.

Cheers!
0
 

Author Comment

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

Assisted Solution

by:Axter
Axter earned 200 total points
ID: 16838303
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

834 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