title: How do I write 2D-array to file using C?
body: <p>I am trying to write a program that has to store an ASCII picture (every line has a different length) in an 2D-Array and then print it out again. Either I have to cut the array at "\n" or I have to create an array with dynamic size. Here is what I have so far. It is printing out in the right way, but every line has 255 chars. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #define MAX_LENGTH 255
  
  int main(void) {
      FILE *iFile, *oFile;
      char elements[MAX_LENGTH][MAX_LENGTH];
      memset(elements, 0, MAX_LENGTH);
      int zeile = 0, position = 0, size = 0;
      char c;
      bool fileend = false;
  
      //open files
      iFile = fopen("image.txt", "r");
      oFile = fopen("imagerotated.txt", "w+");
  
      if ((iFile == NULL) || (oFile == NULL))
      {
          perror("Error: File does not exist.");
          exit(EXIT_FAILURE);
      }
  
  
      //read File to a 2D-Array
      while (1) {
          if ((c = fgetc(iFile)) != EOF) {
              if (c == '\n') {
                  zeile++;
                  position = 0;
              }
              elements[zeile][position] = c;
              position++;
          }
          else {
              fileend = true;
          }
  
          if (fileend == true) {
          break;
          }
      }
  
      //Write 2D-Array into the output file
      fwrite(elements, MAX_LENGTH, MAX_LENGTH, oFile);
  
      fclose(iFile);
      fclose(oFile);  
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p><strong>So my question is what is the best solution to print out an array and cut each line at "\n"? (Or create an array with dynamic length/size).</strong></p>
  
  <p><em>I thought about creating an int countRows and get the number from 'zeile' when fileend becomes true, but how do i get countColoumns?</em></p>
  
  <p>The main goal is rotate the ASCII image in 90 degree steps, but i'm stuck at the output. So i have to use a 2D-Array so i can swap chars easily.
  Thanks for your help.</p>