无法打开具有相对路径的文件?(C ++ ifstream) [英] Cannot open file with relative path? (C++ ifstream)

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

问题描述

我知道这似乎是一个简单的问题,但是我尝试了所有可以想到的事情,但这些事情本来就不应该成为问题.

I know this seems like a simple question, but I tried everything I can think of to no avail to something that shouldn't have been a problem in the first place.

这是一个打开文件的小型C ++程序.当我使用其绝对文件路径打开它时,它可以正常工作.但是,如果使用相对路径,它将停止工作.

This is a small C++ program that opens a file. When I open it with its absolute filepath, it works fine. With a relative path, however, it stops working.

这是程序的文件路径以及我要读取的文件:

Here's the file path of the program and the files I'm trying to read:

C ++程序:"/Users/Baggio/C ++/Lab0/Lab0/Lab0/main.cpp"

文件:/Users/Baggio/C ++/Lab0/Lab0/Lab0/result.txt /Users/Baggio/C ++/Lab0/Lab0/Lab0/dict.txt

以下是代码段:

#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string> 
#include <cstdlib> 
using namespace std;

int main(int argc, const char * argv[]) {

//    string dict_filename = "/Users/Baggio/C++/Lab0/Lab0/Lab0/dict.txt";
//    string result_filename = "/Users/Baggio/C++/Lab0/Lab0/Lab0/result.txt";

    string dict_filename_string = "dict.txt";
    string result_filename_string = "result.txt";

    const char* dict_filename = dict_filename_string.c_str();
    const char* result_filename = result_filename_string.c_str();

    //  open files
    ifstream dict_file(dict_filename, ifstream::in);
    ifstream result_file(result_filename, ifstream::in);

    if (!dict_file || !result_file) {
        cerr << "File could not be opened." << endl;
        exit(1);
    }
}

执行结果

File could not be opened.

我确定我已经对ifstream构造函数参数完成了所有包含权和对数据类型的权利.我唯一值得一提的是我所使用的系统:我在Mac上,并且正在使用XCode6作为我的IDE.

I'm sure I've done all the includes right, and the data types right for the ifstream constructor arguments. The only thing I can think of worth mentioning is the system I'm on: I'm on a Mac and I'm using XCode6 as my IDE.

此外,我尝试将文件的位置(results.txt和dict.txt)移动到这些位置也无济于事:

Also, I've tried to move the files' location (results.txt and dict.txt) to these locations to no avail:

/Users/Baggio/C++/Lab0/Lab0/Lab0/

/Users/Baggio/C++/Lab0/Lab0/

/Users/Baggio/C++/Lab0/

/Users/Baggio/C++/

感谢您的帮助!!任何建议或想法表示赞赏.

Thanks for your help guys!! Any suggestions or thoughts appreciated.

推荐答案

在运行程序时打印出当前的工作目录:

Print out your current working directory when you run the program:

char buffer[256];
char *val = getcwd(buffer, sizeof(buffer));
if (val) {
    std::cout << buffer << std::endl;
}

这将告诉您在哪里运行程序,以及为什么该路径与相对路径不匹配.相对路径是相对于当前工作目录的,而不是相对于您的二进制文件所在的位置的.

This will tell you where you are running your program from and thus why the path doesn't match for relative paths. A relative path is relative to the current working directory, not to where your binary is located.

如果要使路径相对于二进制文件的位置,则必须自己执行.许多编程语言都将此作为选项提供,但它不是C ++内置的.您可以通过使用 main 中的 argv [0] 查找可执行文件来实现.然后,您需要删除可执行路径的文件组件,并将其替换为您感兴趣的文件名.

If you want to make the path relative to the location of the binary then you will have to do that yourself. Many programming languages offer this as an option, but it is not built-in to C++. You can do this by finding the executable using the argv[0] from main. Then you need to drop the file component of the executable path and replace it with the file name that you are interested in.

从C ++ 17开始,您可以使用 std :: filesystem::current_path() 而不是 getcwd .

Since C++17, you can use std::filesystem::current_path() instead of getcwd.

std::cout << std::filesystem::current_path() << std::endl;

这篇关于无法打开具有相对路径的文件?(C ++ ifstream)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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