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. 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");
}
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
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/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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';...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
| |