在C ++中按完整路径打开文件 [英] Open file by its full path in C++

查看:51
本文介绍了在C ++中按完整路径打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户提供文件存在的完整路径,而不仅仅是文件名.如何以这种方式打开文件?

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?

是这样的吗?

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

这似乎不起作用.

推荐答案

在Windows中,通常使用反斜杠字符作为路径分隔符.所以:

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

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

请记住,以C ++源代码编写时,必须使用双反斜杠,因为反斜杠字符本身在双引号引起来的字符串中表示特殊的含义.因此,上面提到的是文件 C:\ Demo.txt .

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.

这篇关于在C ++中按完整路径打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆