473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,"in ner test\n");

}

int main(void){
int i = -5;
char *str=(char*)mal loc(sizeof(char )*100);
sprintf(str,"\n umber 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 5719

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

}

int main(void){
int i = -5;
char *str=(char*)mal loc(sizeof(char )*100);
sprintf(str,"\n umber 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,"in ner test\n");

}

int main(void){
int i = -5;
char *str=(char*)mal loc(sizeof(char )*100);
sprintf(str,"\n umber 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,"in ner test\n");
}

int main(void){
int i = -5;
char *str=(char*)mal loc(sizeof(char )*100);
sprintf(str,"\n umber 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.c om, 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/googlegroupsrep ly/>

Mar 20 '06 #4

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

Similar topics

17
6545
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 seems to be ok..I can write the variable buffer back to a file and the contents is ok. So I think until here all goes fine... But now I want write the buffer contents to a Memo-Box for displaying.
18
1788
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 the email SEND attempts. Theygo to the main Outlook Express window and click Tools, Options. A smaller window opens and they click its 'Security' tab. Then they UNCHECK the box that says "Warn me when other applications try to send mail as me."...
9
1934
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 Public Sub New(ByVal value As String)
3
20287
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'; buffer = 'B'; buffer = 'C';
1
7292
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 to Google Map. If you don't know elisp, first take a gander at Elisp Basics. I often write travelogs on my website. If i traveled to Las Vegas, then
3
2089
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 std::string. The way I see it I could (assuming infile is a valid std::ifstream) char buffer ; while (!infile.eof () && !infile.bad ()) { // Get the next line in the file infile.getline (buffer, 4095); // If fail bit is set it means we...
14
2172
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 times slower than corresponding C functions) it's too expensive for my program. is there any way( library?) to do this fast and safely, please ?
17
2160
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 64-bits of the pointer. does %l exist in both printf and scanf for this purpose ?
12
12451
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 usually use GetBuffer for that LPTSTR CString::GetBuffer( int nMinBufLength )
3
1551
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 java.io.IOException; import java.io.FileOutputStream; import java.io.BufferedInputStream; import java.nio.ByteBuffer; import java.awt.Toolkit; import java.awt.Graphics;
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.