Open file by its full path in C++

前端 未结 5 2292
暖寄归人
暖寄归人 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 22:16

    Normally one uses the backslash character as the path separator in Windows. So:

    ifstream file;
    file.open("C:\\Demo.txt", ios::in);
    

    Keep in mind that when written in C++ source code, you must use the double backslash because the backslash character itself means something special inside double quoted strings. So the above refers to the file C:\Demo.txt.

提交回复
热议问题