c - 在 C 中使用 Linux 上的相对路径打开文件
问题描述
我是 C 编程的新手。我有以下目录结构。
Project
|____ dataset
| |_____ data.txt
|
|____ main.c
我的代码:
FILE *stream = fopen("dataset/data.txt", "r");
或者
FILE *stream = fopen("./dataset/data.txt", "r");
返回此错误:No such file or directory
但是当我使用绝对路径时,它可以正常工作:
FILE *stream = fopen("/home/<user_name>/C/<project_name>/dataset/data.txt", "r");
我在这里做错了什么?