Is it possible to do the following as you can for a C++ string. I can't seem to do this for a Cstring? That is, add characters to the Cstring the same way?


// lets assume this vector is full of characters
vector<char> charvec;
string str;
const char* Cstring;

for (int i = 0; i < charvec.size(); i++)
str+= charvec[i]; // this works fine.

Cstring+= charvec[i] // does not work?  Is there a way of achieving this?


Recommended Answers

All 8 Replies

In your example, Cstring is a pointer to a char, and you need to work on what it's pointing at; not on the pointer.

The operator += applied to a pointer carries out pointer arithmetic; the operator += applied to a C++ string carries out whatever functionality the C++ string class has created for that operator (which is concatenation).

The function strcat in <cstring> may be what you're looking for. That is, of course, massively dangerous and various systems provide various safer functions; strncat, strlcat and others. Alternatively, snprintf and family.

As a general rule of thumb, prefer working with a proper string class instead of naked char arrays.

Thank you for explaining this. In this case I have to use const char pointer but I do prefer to use a string class. strcat seems to be difficult to implement in my case but with some work it will more than likely be possible. I was hoping there was a solution which could closely miming the c++ string += operation.

maybe something like this:

void mystrcat(char* dest, const char* source)
      *dest++ = source++;
   *dest = 0;

Thanks.. I've used this same principal though I'm thinking I have another problem.

I'm reading values from a file into a vector.

I know the pricess is a bit round about, the long way but I need to do it this way. The problem I am having is that I'm reading encrypted data from a file and going through the below process to add it to the const char pointer.

The problem is, it doesn't copy all the data across. Could it be due to a character in the data. I also find if I transfer the values to a String from the vector and then transfer to the const char pointer like this;

The strange thing is, the C++ string displays all the data but when I transfer it to the const char pointer as per below, moconstchar only takes in about one quarter of it?

myconstchar = string.c_str();

The sam problem happens. Any idea why this may be?

vector<char> container;

    while(,1)) // reading from a file into a vector character by character

    // I later on have to transfer this into a const char* ptr
    // I have done this by the following code.

    const char* myconstchar;
    char* fist =  new char;
    char* second =  new char;
    first  = second;

    for (int i = 0; i < container.size(); i++)
    *second = container[i];

    // transfer values from first char to myconstchar

    myconstchar = first;

    cout << *myconstchar; // display const char* myconstchar contents.

const char* myconstchar; myconstchar is a pointer to a char

*myconstchar is that pointer, dereferenceed, so it's a single char.

cout << *myconstchar; is thus a request to show a single char.

Sorry, typo there. It should be:

cout << myconstchar << endl;

I get some of the data but not all of it.

I suspect there's a zero in it, then. If you're reading encrypted data and storing it in a char array, when you use cout to show it, it will be treated as a c-style string and the end of the data is the first zero it comes to.

Hah.. I thought that was it.. I think you're right too.

Thanks very much for the help.

Be a part of the DaniWeb community

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