title: how to implement buffer management for printf in C
body: <p>I believe my issue stems from the <code>printf</code> print buffer, but I know too little about C or buffers to know how to handle this.  I have a simple program written and it just prints a bunch of text (chars/strings) based on some <code>if</code>/<code>else</code> statements depending on the file that is input.  If I call it from a Unix shell like so:</p>
  
  <pre><code>gcc -o myProgram myProgram.c
  ./myProgram fileName
  </code></pre>
  
  <p>It will print out correctly for only the first run.  If i reiterate these steps and once again run the program, it prints out a bunch of "bash" commands in between the output <code>printf</code>s.  Is there an easy way to debug or fix this issue?  I'm very new to C, if you couldn't tell, and I'm using this as part of a school assignment so I am trying to actually figure this out and understand this concept, such that I can then apply it back into my actual program assignment.</p>
  
  <p>EDIT: This is a make-shift example to try to demo my issue<br>
  Note: The file inputted just contains text.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct S{
      char word[30];
  }s;
  
  int main(int argc, char ** argv)
  {
      void print(struct S *s);
      int i; 
      FILE *f = fopen(argv[1], "rb");
      fseek(f, 0, SEEK_SET);
      fread(&amp;s, sizeof(s), 1, f);
      print(&amp;s);
      fclose(f);
  }
  
  void print(struct S *s)
  {
      int i = 0;
      printf("Word: ");
      for(i = 0; i &lt; 30; i++)
      {
          if(s-&gt;word[i] != '\0')
          {
              printf("%c", s-&gt;word[i]);
          }       
          else
          {
              break;
          }
      }
      printf("\n");
  }
  </code></pre>