Results 1 to 5 of 5

Thread: filestreams and strings

  1. #1
    Join Date
    Sep 2004
    Posts
    2,461

    Default filestreams and strings

    If i havea file name ina string(which is in a vector) how can i get the file to be opened by the stream without throwing an error?

  2. #2
    Join Date
    Jan 2009
    Posts
    2,215
    Country
    United States

    Default

    What kind of vector are you using? And if you're using a C++ string (include <string>), you can try including <string.h> too and pass a C string to the vector using the .c_str() converter. That should work. Try something like this:

    Code:
    ...
    vector<string> v;
    v.push_back( file_name );
    ifstream file_handle( v[ 0 ].c_str() )
    ...
    The 0 would of course be the index of the string you want.
    Last edited by Skele Drew; March 20, 2009 at 11:02 PM.
    Rooted HTC Amaze 4G, Android 4.1.2 PA/AOKP/CM10 on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 4.0.3 Android Revolution HD; Laptop: HP HDX, 2.13GHzx2, 4GB, 128GB SSD, 1TB xHDD, Win7
    Facebook page: Skeleville Software Solutions

  3. #3
    Join Date
    Sep 2004
    Posts
    2,461

    Default

    Code:
    ifstream html_file;
        
        for ( int i=0; i < filelist.size(); i++)
        {
             html_file.open (filelist[i], ifstream::out);//getting compile error here
             if(html_file.is_open())
             {
                  //proceed
             }
             
             else
             {
                 cout<<"File "<<filelist[i]<<" does not exist\n";
             }
        }

  4. #4
    Join Date
    Jan 2009
    Posts
    2,215
    Country
    United States

    Default

    Quote Originally Posted by [I.R.A]_FBi View Post
    Code:
    ifstream html_file;
        
        for ( int i=0; i < filelist.size(); i++)
        {
             html_file.open (filelist[i], ifstream::out);//getting compile error here
             if(html_file.is_open())
             {
                  //proceed
             }
             
             else
             {
                 cout<<"File "<<filelist[i]<<" does not exist\n";
             }
        }
    Zap the second argument completely.
    Rooted HTC Amaze 4G, Android 4.1.2 PA/AOKP/CM10 on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 4.0.3 Android Revolution HD; Laptop: HP HDX, 2.13GHzx2, 4GB, 128GB SSD, 1TB xHDD, Win7
    Facebook page: Skeleville Software Solutions

  5. #5
    Join Date
    Sep 2004
    Posts
    2,461

    Default

    the vector neeeds a c_str() .. thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •