passing a string as file name to a ifstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    passing a string as file name to a ifstream

    I want to pass a string as file name to a ifstream object, but I can't.
    Code:
    char cnt_string [6] = "";
    string f = "ss_";
    int cnt = 1;
    string seq = itoa(cnt, cnt_string, 10);
    string file = f+seq+".txt";
    
    ifstream fin( file );
    The error at last line is:
    error C2664: 'std::basic_ifs tream<_Elem,_Tr aits>::basic_if stream(const char *,std::ios_base ::openmode,int) ' : cannot convert parameter 1 from 'std::string' to 'const char *'
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Use the member function std::string::c_ str()

    Comment

    • mahmoodn
      New Member
      • May 2009
      • 77

      #3
      Yes along with sstream header file it is very interesting
      Code:
      #include <sstream>
      ....
      string w1 = "seq_";
      int fileno = 1;
      string filename;
      stringstream str;
      str << w1 << fileno++ << ".txt";
      filename = str.str();
      ofstream fout( filename.c_str() );
      ...

      Comment

      Working...