Convert char array to std::string

I have this code:

char shaderName[128] = "";//the name of the file
char text[1024 * 16] = "";//the content of the file
std::ofstream myfile;//the file
myfile << text;//put the content of the text array in the file
myfile.close();//close the file


This gives me an error. It tells me i can't convert a char array to a string or const char*. I want to be able to concatenate the nameof the file with the .shader extension. I also tried to cast shaderName to an (std::string) or a const char*

EDIT:

ImGui::Begin("Shader Writer");

char text[1024 * 16] = {""};

ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-1.0f, ImGui::GetWindowHeight()), ImGuiInputTextFlags_AllowTabInput | (false ? ImGuiInputTextFlags_ReadOnly : 0));

ImGui::SameLine(0.0f, 2.0f);

if(ImGui::Button("Create", ImVec2(50, 20)))
{

std::ofstream myfile;

std::string FileName = name + ".shader";

myfile.open(FileName.c_str());
myfile << text;
myfile.close();
}

ImGui::End();


The InputTextMultiline() function let's you modify,write,read a text. The "text" in this case is a char array(this function outputs a char array). Then we have another InputTextMultiline() function which also returns a char array. In order to use this data returned by these functions, i need to convert them to strings(or maybe to const char*,even tho i think std::strings is better).

Here is a rapresentation of what these functions do:

– user1430
Nov 2 '17 at 20:42
• But your declaration of "shaderName" inside the if() is suspicious as it hides the outer variable that you presumably filled using the InputText line.
– user1430
Nov 2 '17 at 20:45
• Yes, i corrected it in the actual code but not in the question. I apologize for that
– user100681
Nov 2 '17 at 21:58

This gives me an error. It tells me I can't convert a char array to a string or const char*. I want to be able to concatenate the name of the file with the .shader extension. I also tried to cast shaderName to an (std::string) or a const char*.

You can't concatenate C-style strings using +. If you want to use operator+ you'll have to use std::string. Otherwise you'll have to use strcat().

std::string shaderName = "";
...
std::ofstream myfile;
...


Note that open() does not accept std::string until C++11; prior to that you'll need to concatenate the name into a local variable and use c_str():

std::string fileName = shadername + ".shader";
...
myfile.open(fileName.c_str());


If you must use strcat instead:

char shaderName[128] = "";

It is your responsibility to ensure shaderName is large enough to hold the resulting buffer.