I'm trying to copy a string into a character array to be used in .write(). Here is a bit of my code to show you what I mean:

//input_buffer is a string that is determined during runtime
            int length = 5;
            char * buffer = new char [input_buffer.size()];
            buffer = input_buffer;
            ofstream os;
            os.open ("test.txt", ios::binary );//FDT_filename[top->a0], ios::binary );
            os.write (buffer,length);

I get the following error when I try this:

error: cannot convert ‘std::string’ to ‘char*’ in assignment

Any ideas about how to do this? I realize my notation is likely incorrect.

You need the .c_str() function. If you have a

std::string MyString;

and you want to pass it to something that accepts a char*, pass it MyString.c_str()


Works just the way I wanted. Thanks!

Be a part of the DaniWeb community

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