1
\$\begingroup\$

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.open(shaderName + ".shader");//i want to concatenate the file name with the .shader extension
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] = {""};

char shaderName[128] = {""};

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

    ImGui::Text("Shader Name: ");
    ImGui::InputText("", shaderName, IM_ARRAYSIZE(shaderName));

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

    if(ImGui::Button("Create", ImVec2(50, 20)))
    {
        char shaderName[128] = "";

        std::ofstream myfile;

        std::string name(shaderName);
        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:

\$\endgroup\$
  • \$\begingroup\$ Your edit doesn't say what your new error is? \$\endgroup\$ – Josh Nov 2 '17 at 20:42
  • \$\begingroup\$ But your declaration of "shaderName" inside the if() is suspicious as it hides the outer variable that you presumably filled using the InputText line. \$\endgroup\$ – Josh Nov 2 '17 at 20:45
  • \$\begingroup\$ Yes, i corrected it in the actual code but not in the question. I apologize for that \$\endgroup\$ – user100681 Nov 2 '17 at 21:58
1
\$\begingroup\$

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;
myfile.open(shadername + ".shader");
...

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] = "";
strcat(shaderName, ".shader");
myfile.open(shaderName);

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy