Skip to main content
gamedev.net
🔒 Locked

silly string question

Started by Sander 5 replies 700+ views
Original Post
Sander
Sander
I''m very bad with strings and such in C++ so my question is probabely very n00bish I have a source string as: char cource[256]; and a destination as char dest[40]; Now, I need to copy the contents of source to dest. If source is longer then 40 characters it must simply truncate the strings. Would this function do that for me? sprintf(dest,"%s", source); Thanks in advance! Sander Maréchal [Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]
emilk
emilk
And please call this what it is: C-code
AndreTheGiant
AndreTheGiant
*crocodile dundee accent*

Thats not a string! This is a string:

std::string source;

I dont know if that function (sprintf) would work for your problem; my advice would be to try it.

sbennett
sbennett
quote:
Original post by AndreTheGiant
I dont know if that function (sprintf) would work for your problem; my advice would be to try it.


Using sprintf is generally a bad idea, as it just assumes that the destination buffer is large enough. If it isn''t, then sprintf will simply write straight past it. snprintf is a variant that takes an extra argument, the size of the destination buffer. That''s safer, if it''s available. strncpy is the right one to be using in this case, though.
merlin9x9
merlin9x9
Actually, you need to truncate your strings at 39 characters and not 40, because you always need room for the terminator. You could use code like this to make your life easier:

#include <cstring>

#define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
#define STRING_CAPACITY(s) (ARRAY_LENGTH(s) - 1)

int main()
{
char cource[255 + 1];
char dest[39 + 1];

strncpy(dest, source, STRING_CAPACITY(dest));
}

The ARRAY_LENGTH macro gives you the number of elements that a statically-allocated array can hold. STRING_CAPACITY finds that out for a character array and gives you that value less one, which is the maximum number of characters you can hold safely. Prefer this sort of code to entering the numbers in manually because it's both more flexible and virtually foolproof. Note that I prefer the x + 1 expression to remind myself that my actual length is x and I have an extra space for the terminator.

I'm not positive, but I don't believe that snprintf is standard. I hope it is, because it's strongly preferable to plain sprintf.

Of course, using character arrays in a C++ program is—I think—rather silly. Use std::string, and if you're using code that needs character arrays, simply use string's c_str method to yield the character-array equivalent. For example:

#include <string>
#include <cstdio>

using std::string;
using std:: puts;

int main()
{
string s = "Hello, ";
s += "world!";
puts(s.c_str());
}

By using string, you can use infinitely nicer and more intuitive syntax for manipulation, and never have to worry about size issues. The C++ Standard Library (a.k.a. STL) is your friend, to the degree that the C standard library's equivalent functionality should always be considered obsolete...because it is.

[edited by - merlin9x9 on November 21, 2003 2:45:38 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.