Hi,

I am a newbie to C++ (and programming in general)

I have the following:

char* fOutMsg;
strcpy(fOutMsg, "03DS2");

--------------------------------------------------------------
in another place:

char	fPpn[32]	= {0};
strcpy(fPpn, "                   ");

-------------------------------------------------------------------
in another location:

char   fStr;
struct* ioEx;
strcpy(fStr, W2A(ioEx->authcode));

---------------------------------------------------------------------
more:

char*  iProdType;
strcpy((char*) fDef.prodtype,  iProdType);

------------------------------------------------------------------------
and:

strcpy(fI8_GPID, "%");

So as you can see, I have different parameters for strcpy in different places in the code, I was wondering how to substitute strcpy with strncpy.

How does it work? could you please provide me real examples based on my above data? As I said, I am new to C++ and I don't know how to terminate the buffer with null or not so please be patient with me

Thanks

to look at an online version of the difference between strcpy() and strncpy() go to cppreference.com and look at Standard C string and Char section.

Without further clarification I would expect your first and third versions to fail given that char * needs to have memory assigned to it before it can act as a string and char by itself can never be a string.

Hi,

I am a newbie to C++ (and programming in general)

Since you are new, and have a fresh, open mind, I would recommend that you start using the Standard Template Library now. This library is powerful, helpful, and relatively easy to learn, and it works in all c++ compilers. If you are using a C compiler, you're out of luck.

Anyway, the STL provides a string object that is so much nicer than c character arrays. Here's how you could apply strings to a couple of your examples

using namespace std;
string fOutMsg = "03DS2";

The string object has lots of constructors that make it easy to set it to other strings and character arrays.

string fPpn( 32, ' '  );

You can initialize the string to be a specific character repeated n times

And here's how you can do something like srtncpy:

string superString = "0123456789";
string subString0( superString, 0, 5 );   // Will result in "01234"
string subString1( superString, 2, 5 ); // Will result in "23456"
string subString2( superString, 5, 20 ); // Will result in "56789"

Strings are relatively smart. They know how long they are, so a copy won't try to get data after the end of the string. They also have lots of methods for manipulation back and forth. If you need to convert between numeric values and strings, you can use a stringstream object in a functions like these (i wrote them and use them all the time):

/** Converts a number to a string
  * @param  number - The number to convert
  * @param  prec   - Floating point precision to use
  * @param  w      - Width of the string
  * @return A string representation of the number
  */
template <class T>
std::string num2str( T number, int prec=2, int w=0 ){
    std::ostringstream ss;
    ss.setf( std::ios::showpoint | std::ios::fixed );
    ss.precision( prec );
    ss.fill( ' ' );
    ss.width( w );
    ss << number;
    return ss.str();
}

/** Converts a string to a number
  * @param  str    - The string to convert
  * @param  number - The storage for the number
  */
template <class T>
void str2num( std::string str, T &number ){
    std::istringstream ss( str );
    ss >> number;
}

So, the moral of my story is: If you are going to be learning c++, I would recommend that you take advantage of the STL. It has lots of really helpful stuff that is, in general, much safer and easier to use than some old (and sometimes dangerous) C functions like strcpy, strncpy, atoi, printf, sprintf, etc.

I have the following:

char* fOutMsg;
strcpy(fOutMsg, "03DS2");

--------------------------------------------------------------
in another place:

char	fPpn[32]	= {0};
strcpy(fPpn, "                   ");

-------------------------------------------------------------------
in another location:

char   fStr;
struct* ioEx;
strcpy(fStr, W2A(ioEx->authcode));

---------------------------------------------------------------------
more:

char*  iProdType;
strcpy((char*) fDef.prodtype,  iProdType);

------------------------------------------------------------------------
and:

strcpy(fI8_GPID, "%");

Are you allocating memory for fOutMsg or iProdType before trying to write to it? If not, you'll need to fix that first. And you can't copy a string to the single character fStr.

Why are you planning to use strncpy instead of strcpy? strncpy has caveats:
http://c-faq.com/lib/strncpy.html

>This library is powerful, helpful, and relatively easy
>to learn, and it works in all c++ compilers.

Visual C++ 6 has a notoriously broken standard library (especially the STL). But with a few caveats, you're right:

  1. Compilers that were released before or around 1998 may not conform well at all.
  2. The standard library is updated with every revision of the C++ standard, so be aware what revision your compiler conforms to and what revision the features you want were introduced in.

Thanks everyone for your reply.
I am actually using MS Visual C++ 6.0

Dave, this is only one line of my code and yes I allocated memory for fOutMsg before using it.

Dusk, our code is all COM/DCOM/atl .... and I need to change the strcpy to strncpy because we are facing a compliance issue, so strcpy is not safe and strncpy is required.

Now the thing is that, I cannot tell all the time what my destination buffer size is, in that case, what if the source buffer was larger, and what if it was smaller, what will happen to the remaining "empty" space in my destination buffer?

Will it be easy to move my code from strcpy to strncpy without having to do lots of modifications?

I hope I get clear answers on that thing

Thanks again

Sorry i made a mistake.

I am compiling using MS VS 2005 not VC6. VC6 was originally used but as i mentioned for compliance reason, the code is converted to 2005

Now the thing is that, I cannot tell all the time what my destination buffer size is, in that case, what if the source buffer was larger, and what if it was smaller, what will happen to the remaining "empty" space in my destination buffer?

If you don't know how much space you have available in your destination buffer, you are in big trouble. There are no protections with either strcpy or strncpy, and the program using strcpy or strncpy will happily march off the end of a buffer that is to small. So, if you have a buffer of unknown size, you can never safely copy data into it. You have to establish some way to be sure of your buffer size, or your code will be a ticking time bomb.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.