# Filestreams



## [I.R.A]_FBi (Mar 21, 2009)

ive been reading this

http://www.cplusplus.com/reference/stl/vector/insert.html

but im not getting it. real problem though is say i have a filr name in a vector(of strings) and im trying to input it into a file sttream i get an error


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

i get this error 





> 34 C:\Documents and Settings\0000\My Documents\Assignment\Untitled1.cpp no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >:pen(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::_Ios_Openmode&)'


----------



## FordGT90Concept (Mar 21, 2009)

Where's the lines that declares filelist and populates it?


----------



## [I.R.A]_FBi (Mar 21, 2009)

```
#include<cstdlib>
#include<fstream>
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int files;
vector<string>filelist;
string keyword;
string hold;


int main()
{
    cout<<"Enter Number of Documents > ";
    cin>>files; 
    
    for(int i = 0; i < files; i++)
    {
         cout<<"Enter document "<<i<<" > ";
         cin>>hold;
         filelist.push_back(hold);
    }
    
    cout<<"Enter keyword > ";
    cin>>keyword;
    
    ifstream html_file;
    
    for ( int i=0; i < filelist.size(); i++)
    {
         html_file.open (filelist[i], ifstream::out);
         if(html_file.is_open())
         {
              //proceed
         }
         
         else
         {
             cout<<"File "<<filelist[i]<<" does not exist\n";
         }
    }
    
    

    
    for ( int i=0; i < filelist.size(); i++)
    {
        cout<<filelist[i];
    }

    system("PAUSE");
    return EXIT_SUCCESS;
    
    
}
```


----------



## FordGT90Concept (Mar 21, 2009)

It is a compile error or a run-time error?  Also, if it is a run-time error, does it make it through the first iteration or not?


----------



## [I.R.A]_FBi (Mar 21, 2009)

compile  using dev c 4992


----------



## FordGT90Concept (Mar 21, 2009)

```
html_file.open (filelist[i].c_str(), ifstream::out);
```

You need the .c_str().  "filename" will automatically turn it into a const char but when you hand it a string explicitly, you must explicitly convert it from a string to a const char.


You should also change this line:

```
for ( int i=0; i < filelist.size(); i++)
```
To this:

```
for (unsigned int i=0; i < filelist.size(); i++)
```
vector.size() returns a unsigned int so the index you use should also be unsigned.  It will produce a signed/unsigned mismatch warning if you don't have the unsigned there.


----------



## [I.R.A]_FBi (Mar 21, 2009)

god bless  now on to the file search :|


----------

