title: How to write into a char array in C at specific location using sprintf?
body: <p>I am trying to port some code written in MATLAB to C, so that I can compile the function and execute it faster (the code is executed very often and it would bring a significant speed increase).</p>
  
  <p>So basically what my MATLAB code does it that it takes a matrix and converts it to a string, adding brackets and commas, so I can write it to a text file. Here's an idea of how this would work for a vector MyVec:</p>
  
  <pre><code>MyVec = rand(1,5);
  NbVal = length(MyVec)
  VarValueAsText = blanks(2 + NbVal*30 + (NbVal-1));
  VarValueAsText([1 end]) = '[]';
  VarValueAsText(1 + 31*(1:NbVal-1)) = ',';
  for i = 1:NbVal
     VarValueAsText(1+(i-1)*31+(1:30)) = sprintf('%30.15f', MyVec(i));
  end
  </code></pre>
  
  <p>Now, how can I achieve a similar result in C? It doesn't seem too difficult, since I can calculate in advance the size of my string (char array) and I know the position of each element that I need to write to my memory area. Also the <code>sprintf</code> function exists in C. However, I have trouble understanding how to set this up, also because I don't have an environment where I can learn easily by trial and error (for each attempt I have to recompile, which often leads to a segmentation fault and MATLAB crashing...).</p>
  
  <p>I hope someone can help even though the problem will probably seem trivial, but I have have very little experience with C and I haven't been able to find an appropriate example to start from...</p>