title: C read value from config file
body: <blockquote>
    <p>I need help with reading a port from a config file. The line looks like:
    PORT=8888</p>
  </blockquote>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  
  int main(int ac, char **av)
  {
  
          char buf[256];
          FILE *file = NULL;
          char hostlist[256] = "";
          int port = 8080; /* default not from config */
          int i = 0;
  </code></pre>
  
  <blockquote>
  <pre><code>  // open file
    // some feof code   
  </code></pre>
    
    <p>[..]</p>
  </blockquote>
  
  <pre><code>        strcpy(hostlist,buf);
          if (strstr(buf,"PORT")) {      /* buf[0-3] = */
                  printf("%c\n",buf[5]); /* PORT=8888 */
                  printf("%c\n",buf[6]);
                  printf("%c\n",buf[7]);
                  printf("%c\n",buf[8]);
  </code></pre>
  
  <blockquote>
    <p>This works as expected ^^
       But, when trying to copy into a buffer I get nothing for
       port or I get the default port.</p>
  </blockquote>
  
  <pre><code>                for(i=4;i&lt;9;i++) {
                  while (buf[i] != '\n') {
                  port += buf[i++];
                  printf("buf:%c\n",buf[i]);
                  }
                  }
                  printf("port=%d\n",port);
          }
          fclose(file);
  }
  </code></pre>