Open file by its full path in C++

前端 未结 5 2309
暖寄归人
暖寄归人 2021-02-18 21:24

I want the user to give me the full path where the file exists and not just the file name. How do I open the file this way?

Is it something like this:

if         


        
5条回答
  •  渐次进展
    2021-02-18 21:58

    You can use a full path with the fstream classes. The folowing code attempts to open the file demo.txt in the root of the C: drive. Note that as this is an input operation, the file must already exist.

    #include 
    #include 
    using namespace std;
    
    int main() {
       ifstream ifs( "c:/demo.txt" );       // note no mode needed
       if ( ! ifs.is_open() ) {                 
          cout <<" Failed to open" << endl;
       }
       else {
          cout <<"Opened OK" << endl;
       }
    }
    

    What does this code produce on your system?

提交回复
热议问题