在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");

我在这里做错了什么?

读取path_resolution(7).

您可能希望使用chdir(2),因为fopen(3)调用open(2),并且它们都可能失败。

fopen失败时(或某些系统调用(2)失败)使用errno(3)或perror(3)。

注意glob(3), getcwd(2)和fnmatch(3)。

参见proc(5)。

最新更新