title: Error: C++ literal memory mishandling
body: <p>I have tried a lot to debug my code but it is still not working.The whole code just crashes but there is no allover error I am presenting the code please try to debug that one.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;conio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  void write(char fname[],char text[])
  {
      strcat(fname,".txt");
      ofstream w(fname,ios::app);
      w&lt;&lt;text;
      w&lt;&lt;"\n";
      w.flush();
      w.close();
      cout&lt;&lt;" sippy "&lt;&lt;fname&lt;&lt;"  ";
  }
  int main ()
  {
      int login=0;
      char t[100],id[100]="Its id ",pass[100]="Its password";
      login=1;
      strcpy(t,id);
      strcat(t,"\n");
      strcat(t,pass);
      cout&lt;&lt;" finally ";
      write("database",t);
      getch();
      strcpy(t,id);
      getch();
      cout&lt;&lt;t&lt;&lt;"  showing t here";
      getch();
      cout&lt;&lt;" hope this works for now  ";
      getch();
      cout&lt;&lt;"\nEnter the text"&lt;&lt;endl;
      write(id,t);
  }
  </code></pre>
  
  <p>The above mentioned code does not work on tdm gcc code blocks</p>
  
  <p>Edit 1:</p>
  
  <p>Ok so now the major problem has been detected it is a minor bug usually caused because of drawback of a bad programming style. As it is often suggested that if a string is passed to a function then that particular function allocates a new string at the memory of the passed string. Since the passed string is a literal the code editing the newly formed string would try to edit a read only literal memory which is an error</p>
  
  <p>Literals are read only because if compiler finds the use of same literal at some different place then it would be able to use same memory to flash the contents of literal therefore it becomes a necessity to make a literal read only and use of c-style string carefully(rather std::string should be used)</p>
  
  <p>Thanks to all</p>