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

发布于 2024-09-24 08:52:18 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(4

韵柒 2024-10-01 08:52:18

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

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.)

诺曦 2024-10-01 08:52:18

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

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

在尝试打开文件进行验证之前,请尝试打印 _getcwd 的输出当前工作目录是什么目录。

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.

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).

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

橙味迷妹 2024-10-01 08:52:18

假设你的目录结构如下:

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

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

如果您确实是说文件位于您所说的一个目录中,如下所示:(注意:这是一个尴尬的项目结构)

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

或像这样:(更有意义)

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

那么您需要的路径是 "../ test.txt" 或等效的 "../../project/test.txt"

更好的位置是数据目录,因此您的路径为 “../data/test.txt”

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"

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

我乃一代侩神 2024-10-01 08:52:18

我猜这是一个 Windows VS vbuild(给定反斜杠路径)

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

所以

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

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

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

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.

so

a) copy the file to the right place

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

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