How to make C's fopen function search or create a file in a specific directory?

55 Views Asked by At

When I call the function fopen to create or read a file in a specific directory, it does not work.

If I call the function to read or create the file in the root project, it works very well.

This is my folder structure:

|--project/
|--main.c
|--files/

if I call the function in this way:

FILE *file;
file = fopen("project/files/test.txt", "w");

It does not work. The function cannot create the file;

But if i call in this whay, it works:

FILE *file;
file = fopen("test.txt", "w");

Whay? And how can I do this?

1

There are 1 best solutions below

0
gulpr On

fopen does not create directories.

Your directory structure is:

root/
|--project/
|--main.c
|--files/

"files" files in not a subdirectory of the "project" directory

(I assume that the program is run from the root directory)