C语言 如何使用相对路径打开 C 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3772215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:30:01  来源:igfitidea点击:

How do I open a C file with a relative path?

cdirectory

提问by Thomas

I am trying to parse a txt file and I want to reference it relative to my current directory location. If I put the file path in completely it will work but it I try to use ..\src\test.txt it wont find it. Is this not the proper way to reference a file relative up one directory?

我正在尝试解析一个 txt 文件,我想相对于我当前的目录位置引用它。如果我将文件路径完全放入它会起作用,但我尝试使用 ..\src\test.txt 它不会找到它。这不是在一个目录中引用文件的正确方法吗?

Any help would be appreciated

任何帮助,将不胜感激

回答by zwol

If you put "..\src\test.txt" in a string constant, you need to double all the backslashes so that "\t" and "\s" are not interpreted as escape sequences. (Or you can use forward slashes instead, which is more portable.)

如果将 "..\src\test.txt" 放在字符串常量中,则需要将所有反斜杠加倍,以便 "\t" 和 "\s" 不会被解释为转义序列。(或者您可以改用正斜杠,这样更便于携带。)

回答by Daryl Hanson

It will depend on what the current working directory is set to. By default it is the directory the executable resides in if you double-click the app from Explorer, or the current path the shell is in if started from a command prompt.

这将取决于当前工作目录的设置。默认情况下,如果您从资源管理器中双击应用程序,则它是可执行文件所在的目录,如果从命令提示符启动,则它是 shell 所在的当前路径。

If test.txt is in c:\code\app\src and your application is in c:\code\app, the relative path "..\src\test.txt" is going to end up c:\code\src\test.txt (if launched from explorer).

如果 test.txt 在 c:\code\app\src 中,而您的应用程序在 c:\code\app 中,则相对路径“..\src\test.txt”将以 c:\code\src 结尾\test.txt(如果从资源管理器启动)。

Try printing the output of _getcwdbefore you try opening the file to verify what directory is the current working directory.

在尝试打开文件以验证当前工作目录是哪个目录之前,请尝试打印_getcwd的输出。

回答by Kevin Vermeer

Assuming that your directory structure looks like:

假设您的目录结构如下所示:

project
 +---src
 |       test.txt
 |       proj.c
 \---bin
         a.out  <- Working directory  

your relative path is correct; your working directory is actually on the same level as the text file.

你的相对路径是正确的;您的工作目录实际上与文本文件处于同一级别。

If you really mean that the file is up one directory as you stated, like this: (Note: This is an awkward project structure)

如果你真的是说文件在你所说的一个目录上,就像这样:(注意:这是一个尴尬的项目结构)

project
\---src
    |   test.txt
    |   proj.c
    \---bin
            a.out  

or like this: (makes more sense)

或者像这样:(更有意义)

project
|   test.txt
+---src
|       proj.c
\---bin
        a.out  

Then the path you need is "../test.txt"or, equivalently, "../../project/test.txt"

那么你需要的路径是,"../test.txt"或者等价地,"../../project/test.txt"

A better location would be in a data directory, so your path would be "../data/test.txt"

更好的位置是在数据目录中,所以你的路径是 "../data/test.txt"

回答by pm100

I guess this is a windows VS vbuild (given the back slashed path)

我猜这是一个 windows VS vbuild(考虑到反斜线路径)

VS will have put the binary in something like project\bin\debug. And when you run it in VS it will set the current WD to the location of the binary.

VS 会将二进制文件放在类似 project\bin\debug 的地方。当你在 VS 中运行它时,它会将当前 WD 设置为二进制文件的位置。

so

所以

a) copy the file to the right place

a) 将文件复制到正确的位置

b) change the project properties debug setup to say set the current path to the place where you expect the file to be (relatively)

b) 更改项目属性调试设置以将当前路径设置为您期望文件所在的位置(相对)