GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jun-2004, 15:49
rn2388 rn2388 is offline
New Member
 
Join Date: Jun 2004
Posts: 1
rn2388 is on a distinguished road

sprintf and snprintf question in C


Hi,

My C library doesn't have the snprintf function built in.

I would like to know how to change the following code from
snprintf to sprintf.

int len =strlen(book->name);
snprintf(book->name + len, sizeof(book->name) -len -1, "%s", book->id);

Any help would be appreciated.

rn2388
  #2  
Old 16-Jun-2004, 16:11
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by rn2388
Hi,

My C library doesn't have the snprintf function built in.

I would like to know how to change the following code from
snprintf to sprintf.

int len =strlen(book->name);
snprintf(book->name + len, sizeof(book->name) -len -1, "%s", book->id);

Any help would be appreciated.

rn2388

Hello rn2388. Welcome to GIDForums.

You should be able to replicate this using a quick strcpy and a properly placed null.

CPP / C++ / C Code:
char* temp = (char*) malloc(strlen(book->id);
int len =strlen(book->name);

strcpy(temp,book->id);
*(temp+sizeof(book->name) - len -1) = 0;
sprintf(book->name + len, "%s", temp);

That being said, I am lost in what this is doing. I see no instance in which sizeof(book->name) - len -1 is going to result in a positive number. What am I missing?
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:50.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.