在 C++ 中按完整路径打开文件

发布于 2024-07-20 04:44:04 字数 162 浏览 5 评论 0原文

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

是不是这样的:

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

这似乎不起作用。

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:

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

This doesn't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌伤浅笑 2024-07-27 04:44:05

对这个问题的不同看法,可能会对某人有所帮助:

我来到这里是因为我在 Windows 上的 Visual Studio 中进行调试,并且我对所有这些 /\\ 感到困惑讨论(在大多数情况下这确实不重要)。

对我来说,问题是:“当前目录”未设置为我在 Visual Studio 中想要的内容。 它默认为可执行文件的目录(取决于您如何设置项目)。

更改方式:右键单击解决方案 -> 属性-> 工作目录

我只是提到它,因为这个问题似乎以 Windows 为中心,这通常也意味着以 VisualStudio 为中心,这告诉我这个提示可能是相关的(:

A different take on this question, which might help someone:

I came here because I was debugging in Visual Studio on Windows, and I got confused about all this / vs \\ discussion (it really should not matter in most cases).

For me, the problem was: the "current directory" was not set to what I wanted in Visual Studio. It defaults to the directory of the executable (depending on how you set up your project).

Change it via: Right-click the solution -> Properties -> Working Directory

I only mention it because the question seems Windows-centric, which generally also means VisualStudio-centric, which tells me this hint might be relevant (:

臻嫒无言 2024-07-27 04:44:04

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

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

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

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.

一身仙ぐ女味 2024-07-27 04:44:04

您可以对 fstream 类使用完整路径。 以下代码尝试打开 C: 驱动器根目录中的文件 demo.txt。 请注意,由于这是输入操作,因此该文件必须已经存在。

#include <fstream>
#include <iostream>
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;
   }
}

这段代码在您的系统上会产生什么结果?

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 <fstream>
#include <iostream>
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?

冬天的雪花 2024-07-27 04:44:04

该代码似乎对我有用。 我认为@Iothar 也是如此。

检查是否包含编译所需的标头。 如果已编译,请检查是否存在这样的文件,以及所有内容、名称等是否匹配,并检查您是否有权读取该文件。

要进行交叉检查,请检查是否可以使用 fopen 打开它。

FILE *f = fopen("C:/Demo.txt", "r");
if (f)
  printf("fopen success\n");

The code seems working to me. I think the same with @Iothar.

Check to see if you include the required headers, to compile. If it is compiled, check to see if there is such a file, and everything, names etc, matches, and also check to see that you have a right to read the file.

To make a cross check, check if you can open it with fopen..

FILE *f = fopen("C:/Demo.txt", "r");
if (f)
  printf("fopen success\n");
↘人皮目录ツ 2024-07-27 04:44:04

对于那些动态获取路径的人...例如拖放:

一些主要结构会使用双引号拖放文件,例如:

"C:\MyPath\MyFile.txt"

快速而好的解决方案是使用此函数从字符串中删除字符:

void removeCharsFromString( string &str, char* charsToRemove ) {
   for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
      str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
   }
} 

string myAbsolutepath; //fill with your absolute path
removeCharsFromString( myAbsolutepath, "\"" );

myAbsolutepath 现在仅包含 C:\MyPath\MyFile.txt

该函数需要这些库: ; .

该函数基于此答案

工作小提琴: http://ideone.com/XOROjq

For those who are getting the path dynamicly... e.g. drag&drop:

Some main constructions get drag&dropped file with double quotes like:

"C:\MyPath\MyFile.txt"

Quick and nice solution is to use this function to remove chars from string:

void removeCharsFromString( string &str, char* charsToRemove ) {
   for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
      str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
   }
} 

string myAbsolutepath; //fill with your absolute path
removeCharsFromString( myAbsolutepath, "\"" );

myAbsolutepath now contains just C:\MyPath\MyFile.txt

The function needs these libraries: <iostream> <algorithm> <cstring>.

The function was based on this answer.

Working Fiddle: http://ideone.com/XOROjq

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文