title: Can't make atoi take in a string (string vs. C-string?)
body: <p>I have read a line from a file and I am trying to convert it to an int. For some reason <code>atoi()</code> (convert string to integer) won't accept a <code>std::string</code> as an argument (possibly some issue with strings vs c-strings vs char arrays?) - how do I get <code>atoi()</code> to work right so I can parse this text file? (going to be pulling lots of ints from it).</p>
  
  <p>Code:</p>
  
  <pre><code>int main()
  {
      string line;
      // string filename = "data.txt";
      // ifstream file(filename)
      ifstream file("data.txt");
      while (file.good())
      {
          getline(file, line);
          int columns = atoi(line);
      }
      file.close();
      cout &lt;&lt; "Done" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The line causing problems is:</p>
  
  <pre><code>int columns = atoi(line);
  </code></pre>
  
  <p>which gives the error:</p>
  
  <blockquote>
    <p>error: cannot convert <code>'std::string'</code> to <code>'const char*'</code> for argument '1' to 'int <code>atop(const char*)</code>'</p>
  </blockquote>
  
  <p>How do i make atoi work properly?</p>
  
  <p>EDIT: thanks all, it works! new code:</p>
  
  <pre><code>int main()
  {
  string line;
  //string filename = "data.txt";
  //ifstream file (filename)
  ifstream file ("data.txt");
  while ( getline (file,line) )
  {
    cout &lt;&lt; line &lt;&lt; endl;
    int columns = atoi(line.c_str());
    cout &lt;&lt; "columns: " &lt;&lt; columns &lt;&lt; endl;
    columns++;
    columns++;
    cout &lt;&lt; "columns after adding: " &lt;&lt; columns &lt;&lt; endl;
  }
  file.close();
  cout &lt;&lt; "Done" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>also wondering why 
      string filename = "data.txt";
      ifstream file (filename)
  fails, but</p>
  
  <pre><code>    ifstream file("data.txt");
  </code></pre>
  
  <p>works?
  ( I will eventually be reading filename form the command line so need to make it not a string literal)</p>