std::basic_ifstream<CharT,Traits>:: basic_ifstream
| 
           
            basic_ifstream
            
             (
            
            
             )
            
            
             ;
            
           
           | (1) | |
| 
           
            
             explicit
            
            basic_ifstream
            
             (
            
            
             const
            
            
             char
            
            
             *
            
            filename,
             
             
              
               std::
               
                ios_base
               
               
                ::
               
               
                openmode
               
              
             
             mode
              | (2) | |
| 
           
            
             explicit
            
            basic_ifstream
            
             (
            
            
             const
            
            std
            
             ::
            
            
             filesystem
            
            
             ::
            
            
             path
            
            
             ::
            
            
             value_type
            
            
             *
            
            filename,
             
             
              
               std::
               
                ios_base
               
               
                ::
               
               
                openmode
               
              
             
             mode
              | (3) | (since C++17) | 
| 
           
            
             explicit
            
            basic_ifstream
            
             (
            
            
             const
            
            
             
              std::
              
               string
              
             
            
            
             &
            
            filename,
             
             
              
               std::
               
                ios_base
               
               
                ::
               
               
                openmode
               
              
             
             mode
              | (4) | (since C++11) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            FsPath
            
             >
            
             
             
              explicit
             
             basic_ifstream
             
              (
             
             
              const
             
             FsPath
             
              &
             
             filename,
              | (5) | (since C++17) | 
| 
           
            basic_ifstream
            
             (
            
            basic_ifstream
            
             &&
            
            other
            
             )
            
            
             ;
            
           
           | (6) | (since C++11) | 
| 
           
            basic_ifstream
            
             (
            
            
             const
            
            basic_ifstream
            
             &
            
            rhs
            
             )
            
            
             =
            
            delete
            
             ;
            
           
           | (7) | (since C++11) | 
Constructs new file stream.
         open()
        
       
       call returns a null pointer, sets
       
        
         setstate
         
          (
         
         failbit
         
          )
         
        
       
       .
       
        
         Overload
         
          (3)
         
         is only provided if
         
          
           std
           
            ::
           
           
            filesystem
           
           
            ::
           
           
            path
           
           
            ::
           
           
            value_type
           
          
         
         is not
         
          
           char
          
         
         .
        
        
         
          (since C++17)
         
        
       
      
          FsPath
         
         is
         
          
           std::filesystem::path
          
         
         .
        
        
         
          (since C++17)
         
        
       
      
         rdbuf()
        
       
       pointer), then move-constructs the
       
        
         std::basic_filebuf
        
       
       member, then calls
       
        
         this
         
          -
         
         
          >
         
         set_rdbuf
         
          (
         
         
          )
         
        
       
       to install the new
       
        basic_filebuf
       
       as the
       
        
         rdbuf()
        
       
       pointer in the base class.
      Parameters
| filename | - | the name of the file to be opened | ||||||||||||||||
| mode | - | specifies stream open mode. Following constants and bit-wise OR between them may be used: 
 | ||||||||||||||||
| other | - | another file stream to use as source | 
Example
#include <fstream> #include <string> #include <utility> int main() { std::ifstream f0; std::ifstream f1("test.bin", std::ios::binary); std::string name = "example.txt"; std::ifstream f2(name); std::ifstream f3(std::move(f1)); }
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior | 
|---|---|---|---|
| LWG 3430 | C++17 | std::filesystem::path overload led to unwanted conversions | avoided by making it a template | 
See also
| opens a file and associates it with the stream (public member function) | |
| opens a file and configures it as the associated character sequence (public member function of 
           std::basic_filebuf<CharT,Traits>
          ) | |
| replaces the 
          rdbuf
         without clearing its error state(protected member function) | |
| constructs the object (public member function of 
           std::basic_istream<CharT,Traits>
          ) |