title: How to open a file ending with a particular extension in C++
body: <p>I am trying to write a lexer using flex, and want to open and read from a file ending with a particular extension. E.g <code>filename.k</code>. I am only able to do it if I specify the file name as well as the extension.</p>
  
  <pre><code>FILE *myfile = fopen("a.k", "r");
          if (!myfile) {
          cout &lt;&lt; "I can't open a.k!" &lt;&lt; endl;
  </code></pre>
  
  <p>Can someone show me the way to open <code>*.k</code> files in C++.</p>
  
  <p>I am running flex on Ubuntu. What I am trying to do is to run a flex program. The above code executes fine. I wanted a way where I can open a file with <code>.k</code> extension irrespective of the file name. Example. <code>./myprogram a.k</code> or <code>./myprogram b.k</code>. In the above example I always have to specify the file name in the code itself all the time. </p>