title: Get the length of each line in file with C and write in output file
body: <p>I am a biology student and I am trying to learn perl, python and C and also use the scripts in my work. So, I have a file as follows:</p>
  
  <pre><code>&gt;sequence1
  ATCGATCGATCG
  &gt;sequence2
  AAAATTTT
  &gt;sequence3
  CCCCGGGG  
  </code></pre>
  
  <p>The output should look like this, that is the name of each sequence and the count of characters in each line and printing the total number of sequences in the end of the file.</p>
  
  <pre><code>sequence1 12
  sequence2 8
  sequence3 8
  Total number of sequences = 3
  </code></pre>
  
  <p>I could make the perl and python scripts work, this is the python script as an example:</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  
  my_file = open(sys.argv[1]) #open the file
  my_output = open(sys.argv[2], "w") #open output file
  
  total_sequence_counts = 0
  
  for line in my_file: 
      if line.startswith("&gt;"):
          sequence_name = line.rstrip('\n').replace("&gt;","") 
          total_sequence_counts += 1 
          continue    
      dna_length = len(line.rstrip('\n')) 
      my_output.write(sequence_name + " " + str(dna_length) + '\n')
  my_output.write("Total number of sequences = " + str(total_sequence_counts) + '\n')
  </code></pre>
  
  <p>Now, I want to write the same script in C, this is what I have achieved so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char *argv[])
  {
  input = FILE *fopen(const char *filename, "r");
  output = FILE *fopen(const char *filename, "w"); 
  
  double total_sequence_counts = 0;
  char sequence_name[];
  
  char line [4095]; // set a temporary line length
  char buffer = (char *) malloc (sizeof(line) +1); // allocate some memory
  
  while (fgets(line, sizeof(line), filename) != NULL) { // read until new line character is not found in line
  
      buffer = realloc(*buffer, strlen(line) + strlen(buffer) + 1); // realloc buffer to adjust buffer size
      if (buffer == NULL) { // print error message if memory allocation fails
          printf("\n Memory error");
          return 0;
      }
      if (line[0] == "&gt;") {
          sequence_name = strcpy(sequence_name, &amp;line[1]); 
          total_sequence_counts += 1
          }
          else {
              double length = strlen(line);
              fprintf(output, "%s \t %ld", sequence_name, length);
          }
      fprintf(output, "%s \t %ld", "Total number of sequences = ", total_sequence_counts);
  }
      int fclose(FILE *input); // when you are done working with a file, you should close it using this function. 
      return 0;
      int fclose(FILE *output);
      return 0;
  }
  </code></pre>
  
  <p>But this code, of course is full of mistakes, my problem is that despite studying a lot, I still can't properly understand and use the memory allocation and pointers so I know I especially have mistakes in that part. It would be great if you could comment on my code and see how it can turn into a script that actually work. By the way, in my actual data, the length of each line is not defined so I need to use malloc and realloc for that purpose.</p>