473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to make a string buffer...

s88
Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;
I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?
Thank you!

Dave.

Mar 20 '06 #1
3 5686

s88 wrote:
Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;
I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?


Not sure what you mean by "String Buffer", but if this is a specific
problem, you can just use strcat in "temp" function.

void test(char * str)
{
strcat(str," inner test\n");
}

Mar 20 '06 #2
s88 wrote:
Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;
I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?


Don't cast the return value of malloc and check out the strcat
function.

Robert Gamble

Mar 20 '06 #3
Robert Gamble wrote:
s88 wrote:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");
}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;
}

I want the char array str contains "\nnumber is -5\ninner test\n",
but the result is "\ninner test\n". I know the str means that the
beginning pointer of the cahr array. So, as I invoke the test
function, the sprintf of teh test function write down the
characters from the beginning of the str.
How to slove this problem?


Don't cast the return value of malloc and check out the strcat
function.


Better, check out the (non-standard) strlcpy and strlcat
functions. You can find an implementation in pure portable C, with
references to the original proposals, etc., at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Mar 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Axel | last post by:
Hiho, here my Newbie question (Win32 GUI): I'm reading a file in binary mode to a char* named buffer. I used malloc(filesize) to make the needed space avaiable. The filedata in the buffer...
18
by: MLH | last post by:
I have an A97 app that automatically sends eMail using OutLook Express. Each installation requires the user to reconfigure Outlook Express to avoid prompts waiting for them to authorize or deny...
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
3
by: Jim Langston | last post by:
Is it possible to initialize a std::string with a character array, not neccessarily null terminated? I.E. Given something like this: char buffer; buffer = 0x01; buffer = 0x00; buffer = 'A';...
1
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link...
3
by: Benny the Guard | last post by:
I have a comma delimited file to parse up. So I figured I will go line by line and parse them up. Now efficiency is the top priority here. So I figure I can use the old-school C approach or use...
14
by: Aman JIANG | last post by:
hi i need a fast way to do lots of conversion that between string and numerical value(integer, float, double...), and boost::lexical_cast is useless, because it runs for a long time, (about 60...
17
by: let_the_best_man_win | last post by:
How do I print a pointer address into a string buffer and then read it back on a 64 bit machine ? Compulsion is to use string (char * buffer) only. printing with %d does not capture the full...
12
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i...
3
by: PeterDoh | last post by:
im in the creation of an sprite packer for a game, but i can't get it to run? a friend sent me the source for it, so i have no idea what to do from this? so far: import java.io.File; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.