title: Why is snprintf not reading after %s?
body: <p>I was trying to write a program that checks out if a location exists under the <strong>/home/user directory</strong>. To do that, I had to get the username with the <code>whoami</code> command and add the output of it to the buffer to use the <code>locate</code> command.</p>
  <p>However, even though the <code>snprintf</code> read the <code>whoami</code> command, it didn't read the rest. I made a couple of searches and came to a result that <code>NULL</code> may not be terminated at the end of the string. Nevertheless, I couldn't find out how to terminate it manually. I am not sure what the problem is, so, here I am.</p>
  <p>Here is the code for a better demonstration of my issue:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
      
  char *readFile(char *);
  bool check();
      
  bool check() {
      char path[200] = { 0 };
      
      snprintf(path, 200, &quot;/home/%s/.example&quot;, readFile(&quot;whoami&quot;));
      
      char lll[300] = { 0 };
      
      snprintf(lll, 300, &quot;locate %s&quot;, path);
      
      char *buffer = readFile(lll);
      
      if (strcmp(buffer, path) == 0) {
          return true;
      } else {
          return false;
      }
  }
  
  char *readFile(char cmd[200]) {
      char cmd1[99999] = { 0 };
      system(&quot;touch cmd&quot;);  
      snprintf(cmd1, 99999, &quot;%s &gt;&gt; cmd&quot;, cmd);
      system(cmd1); 
          
      FILE *f = fopen(&quot;cmd&quot;, &quot;rt&quot;);
      assert(f);
      fseek(f, 0, SEEK_END);
      long length = ftell(f);
      fseek(f, 0, SEEK_SET);
      char *buffer = (char *)malloc(length + 1);
      buffer[length] = '\0';
      fread(buffer, 1, length, f);
      fclose(f);
      system(&quot;rm cmd&quot;);
      return buffer;
  }
      
  int main() {
      int x = check();
      
      if (x == 1)
          printf(&quot;There is a location like that&quot;);
      else
          printf(&quot;There isn't&quot;);
      
      return 0;
  }
  </code></pre>