954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem understanding Sprintf

Hello

Can some one please explain what this statement is
I am very new to C++ and VC++ statement

sprintf(DirString,"%s/cl_log.csv",DirName);
I could not understand what second argument

Thanks in advance,
Durga

coolgirl
Newbie Poster
2 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

If you're using C++, sprintf is not the right tool. Now here's the answer.

The second argument is a format string. The sprintf subroutine copies the format string to the bytes of memory pointed to by DirString.

The thing is, it replaces "%s" with the contents of the string DirName. The percent sign is given special interpretation, and %s makes the subroutine insert the characters of a string.

Other characters can be used behind the percent sign for different meaning. For example, %d will draw the decimal representation of an integer. E.g.

sprintf(DirString, "%s/cl_log.csv %d", DirName, 478);


The first insertion code, %s, corresponds to DirName, and the second, %d, corresponds to 478. The character array pointed to by DirString will be just like it was in the code you provided, except with " 478" on the end.

You can see a ludicrously (and usefully) in-depth description of all the printf and sprintf options at http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf , but it does not have any examples.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

what is the use of CL_log.csv file

If you're using C++, sprintf is not the right tool. Now here's the answer.

The second argument is a format string. The sprintf subroutine copies the format string to the bytes of memory pointed to by DirString.

The thing is, it replaces "%s" with the contents of the string DirName. The percent sign is given special interpretation, and %s makes the subroutine insert the characters of a string.

Other characters can be used behind the percent sign for different meaning. For example, %d will draw the decimal representation of an integer. E.g.

sprintf(DirString, "%s/cl_log.csv %d", DirName, 478);

The first insertion code, %s, corresponds to DirName, and the second, %d, corresponds to 478. The character array pointed to by DirString will be just like it was in the code you provided, except with " 478" on the end.

You can see a ludicrously (and usefully) in-depth description of all the printf and sprintf options at http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf , but it does not have any examples.

coolgirl
Newbie Poster
2 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Basically it allows you to create a string with known values and populated values.. perhaps an example like this.
char name[] = "coolgirl";
int num = 10;

sprintf( finalDest, "%s wants to go out %d times today", name, num );

will end up copying "coolgirl wants to go out 10 times today" into finalDest;

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You