title: Command line argument to read text file
body: <p>I want to be able to read text files from the command line but my program is not compiling. Does anyone know what I'm doing wrong? </p>
  
  <p>I'm trying to have the command like accept the menu.txt files and read them and put them into an array but I don't know how to have it read them from the command line </p>
  
  <p>So what I'm trying to do is</p>
  
  <p>1) ./a.out menu1.txt menu2.txt</p>
  
  <p>And let the user choose how many files they want to read from so it could also be</p>
  
  <p>2) ./a.out menu1.txt menu2.txt menu3.txt how do I do that?</p>
  
  <pre><code>menu1.txt 
  hamburger 5.00
  pizza 3.25
  chips 2.50
  
  menu2.txt
  hamburger 2.00
  pizza 2.35
  chips 1.50
  
  menu3.txt
  hamburger 4.00
  pizza 5.35
  chips 0.50
  </code></pre>
  
  <p>This is what i have so far: </p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  int main( int argc, char* argv[] ){
      ifstream inStream;
      ofstream outStream;
      for(int i=1; i &lt;= argc; i++) 
      // i=1, assuming files arguments are right after the executable
      {
          string fn = argv[i]; //filename
          cout &lt;&lt; fn;
          fstream f;
          f.open(fn);
  
  
          //inStream.open("menu1.txt");
          // inStream.open("menu2.txt");
          string item[10];
          double cost[10];
  
  
          // ifstream m1("menu1.txt");
          //ifstream m2("menu2.txt");
  
  
          string name;
          double var;
  
          string line;
          istringstream iss(line);
  
          short loop=0;
          if(f.is_open()){
  
              while(!f.eof()){
                  iss &gt;&gt; name &gt;&gt; var;
                  getline(f, line);
                  while(f&gt;&gt;name&gt;&gt;var){
                      item[loop]=name;
                      cost[loop]=var;
                      cout &lt;&lt; "Item at index " &lt;&lt; loop &lt;&lt; " " &lt;&lt; item[loop]&lt;&lt;endl;
                      cout &lt;&lt; "Cost at index " &lt;&lt; loop &lt;&lt; " " &lt;&lt; cost[loop]&lt;&lt;endl;
                      loop++;
                  }
  
              }
  
          f.close();
          }
  
  
      }
  
  
      return 0;
  
  }
  </code></pre>
  
  <p>These are the error messages when i try to compile: </p>
  
  <pre><code>     p3.cpp: In function ‘int main(int, char**)’:
       p3.cpp:17:25: error: no matching function for call to      ‘std::basic_ifstream&lt;char&gt;::open(std::string&amp;)’
           inStream.open(fn);
                           ^
  p3.cpp:17:25: note: candidate is:
  In file included from p3.cpp:1:0:
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541:7: note: void std::basic_ifstream&lt;_CharT, _Traits&gt;::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::ios_base::openmode = std::_Ios_Openmode]
         open(const char* __s, ios_base::openmode __mode = ios_base::in)
         ^
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string&lt;char&gt;}’ to ‘const char*’
  </code></pre>