title: c++ vector bad access
body: <p>Hi I have the following c++ program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  #include &lt;boost/foreach.hpp&gt;
  #include &lt;stdexcept&gt;
  #include &lt;boost/flyweight.hpp&gt;
  #include &lt;boost/lexical_cast.hpp&gt;
  #include &lt;boost/filesystem.hpp&gt;
  
  namespace fs = boost::filesystem;
  
  struct entry
  {
  int file;
  std::vector&lt;double&gt; a;
  };
  
  
  void my_file(const std::string&amp;file, std::vector&lt;entry&gt; &amp;data, int i){
  try{
      std::ifstream in(file.c_str());
      entry e;
      std::string line;
      e.file = i;
      while(getline(in,line)){
          try{
              data[i].a.push_back( boost::lexical_cast&lt;double&gt; (line));
          }catch(boost::bad_lexical_cast bad){
              //std::cerr &lt;&lt; bad.what() &lt;&lt; std::endl;
          }
      }
  }catch(std::runtime_error err){
      std::cerr &lt;&lt; err.what() &lt;&lt; std::endl;
  }
  
  }
  
  void write_file(const std::string &amp;file,std::vector&lt;entry&gt; data,const char* t_path){
  try{
      std::string new_file = t_path ;
      new_file = new_file + "/" + file;
      std::ofstream f(new_file.c_str());
  
      for(size_t i = 0 ;i &lt; data[1].a.size();i++){
          std::cout &lt;&lt; "i: " &lt;&lt; i;
          for(size_t j = 1; j &lt; data.size();j++){
              std::cout &lt;&lt; "j: " &lt;&lt; j &lt;&lt; std::endl;
              f &lt;&lt; data[j].a[i]&lt;&lt; "\t";
          }
          f &lt;&lt; "\n";
      }
  
  }catch(std::runtime_error err){
      std::cerr &lt;&lt; err.what()&lt;&lt; std::endl;
  }
  }
  
  
  int collect_peak(const char*argv,const char*out){
  std::cout &lt;&lt; "collecting peaks\n";
  std::stringstream sstr(argv);
  std::string _line;
  int c = 0;
  std::vector&lt;std::string&gt; files;
  
  while (getline(sstr,_line)){
      std::cout &lt;&lt; _line &lt;&lt; std::endl;
      fs::path p(_line);
      std::string tmp = p.parent_path().string() +"/_peak_" +      p.filename().string();
      files.push_back(tmp);
      c++;
  }
  
  std::cout &lt;&lt; "c: " &lt;&lt; c &lt;&lt; std::endl;
  std::vector&lt;entry&gt; data;
  for (int i=0 ; i &lt; files.size() ;++i){
      std::cout &lt;&lt; files[i] &lt;&lt;std::endl;
      my_file(files[i],data,i);
  }
  write_file("__peak.txt",data,out);
  return 0;
  
  }
  </code></pre>
  
  <p>Somehow it always gives me a bad access in the my_file method. The program should actually to the following:</p>
  
  <ol>
  <li>read multiple files that contain a caption and ten doubles seperated by a newline</li>
  <li><p>output everything into one file so it looks like this:</p>
  
  <ol>
  <li><p>file \t 2. file \t 3. file \t ...</p></li>
  <li><p>file \t 2. file \t 3. file \t ...</p></li>
  <li><p>file \t 2. file \t 3. file \t ...</p></li>
  <li><p>file \t 2. file \t 3. file \t ...</p></li>
  <li><p>file \t 2. file \t 3. file \t ...</p></li>
  </ol></li>
  </ol>
  
  <p>This actually worked already, but i reused it now in a different program. Any ideas?</p>
  
  <p>Thank you</p>