0

I am using

FILE *fp = fopen("pasta/test.txt",w);

but this line doesn´t create a file in folder "pasta", i am creating a file with name "pasta/test.txt", can you help me?

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
Rhuan Caetano
  • 285
  • 3
  • 9
  • 3
    First thing first: You can't use e.g. `fopen` to create directories, for that you have to use e.g. the [`mkdir`](http://man7.org/linux/man-pages/man2/mkdir.2.html) system call. Secondly, when posting code, please post ***working*** code. See e.g. [how to ask good questions](http://stackoverflow.com/help/how-to-ask) from [the help pages](http://stackoverflow.com/help). – Some programmer dude Jul 02 '15 at 06:11
  • 1
    Did you check the reason for failure? `errno`? – Sourav Ghosh Jul 02 '15 at 06:11
  • what command can i use to create a directories and a file ? – Rhuan Caetano Jul 02 '15 at 06:13
  • Please check this http://stackoverflow.com/questions/16153477/how-to-create-a-file-in-a-specific-directory it will help you. – User2 Jul 02 '15 at 06:47

2 Answers2

1

The answer is in this question here (it is not a duplicate) the distribution you are using is irrelevant, since those are POSIX calls. Google them, is good to know them

Community
  • 1
  • 1
Luis Sieira
  • 29,926
  • 3
  • 31
  • 53
0

You need to check the return value of fopen and use errno. The perror function is quite useful:

FILE *fp = fopen("/pasta/test.txt", "w");
if (fp == NULL) {
   perror("Opening file failed:");
}
Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46