How to create file inside a directory using C

TonyGW

I am trying to create a directory and a file inside the directory. Below is my code in C, but when I try to compile it, I got this error: invalid operands to binary / (have ‘const char *’ and ‘char *’)

char *directory = "my_dir";

struct stat dir = {0};
if(stat(directory, &dir) == -1)
{
    mkdir(directory, 0755);
    printf("created directory testdir successfully! \n");
}

int filedescriptor = open(directory/"my_log.txt", O_RDWR | O_APPEND | O_CREAT);
if (filedescriptor < 0)
{
    perror("Error creating my_log file\n");
    exit(-1);
}

thanks for help

ryyker

use sprintf() or similar to create the pathFilename string:

char pathFile[MAX_PATHNAME_LEN];
sprintf(pathFile, "%s\\my_log.txt", directory );

then

int filedescriptor = open(pathFile, O_RDWR | O_APPEND | O_CREAT);   

Note: If you are using linux, change \\ to / and MAX_PATHNAME_LEN to 260 (or whatever linux likes to use for that value.)

EDIT if you need to check that a directory exists before creating a file there, you can do something like this:

if (stat("/dir1/my_dir", &st) == -1) {
    mkdir("/dir1/my_dir", 0700);
}   

Read more here: stat, mkdir

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a text file inside a specific directory?

From Dev

How to create a text file inside a specific directory?

From Dev

Create directory/file in iOS using C++

From Dev

Create directory/file in iOS using C++

From Dev

Using diff / patch to create a new file inside a directory

From Dev

How to create a new file / directory using ranger?

From Dev

How to create file in special directory using PHP

From Dev

How to create file in some directory C++

From Dev

Zsh: How to create a directory and file inside it with one command?

From Dev

How to create Image File inside Internal Cache Directory in Android

From Dev

How to check if folder exist inside a directory if not create it using python

From Dev

How to move file inside directory, using Linux (Bash)

From Dev

Check if a file is inside a directory in C

From Dev

Check if a file is inside a directory in C

From Dev

How to create inside each directory a .txt file that is called as the directory with find command

From Dev

create file inside directory in bash script

From Dev

Create a file in a directory using python

From Dev

Create a file in a directory using python

From Dev

How to create file in different directory

From Dev

How to create a .txt file using c#?

From Dev

How to read file index of a directory using C/C++?

From Java

How to create a directory using Ansible

From Dev

How to create a temporary directory in C?

From Dev

Create Directory and Store File (C programming)

From Dev

Qt: How to create a directory in project file?

From Dev

File inside a directory script

From Dev

how can I create a file in the current user's home directory using Java?

From Dev

How to create a file example_user.rb in your application root directory in ROR using Sublime Text

From Dev

How can I create an XML file that is a list of files in a target directory using only Ant and XSLT?

Related Related

  1. 1

    How to create a text file inside a specific directory?

  2. 2

    How to create a text file inside a specific directory?

  3. 3

    Create directory/file in iOS using C++

  4. 4

    Create directory/file in iOS using C++

  5. 5

    Using diff / patch to create a new file inside a directory

  6. 6

    How to create a new file / directory using ranger?

  7. 7

    How to create file in special directory using PHP

  8. 8

    How to create file in some directory C++

  9. 9

    Zsh: How to create a directory and file inside it with one command?

  10. 10

    How to create Image File inside Internal Cache Directory in Android

  11. 11

    How to check if folder exist inside a directory if not create it using python

  12. 12

    How to move file inside directory, using Linux (Bash)

  13. 13

    Check if a file is inside a directory in C

  14. 14

    Check if a file is inside a directory in C

  15. 15

    How to create inside each directory a .txt file that is called as the directory with find command

  16. 16

    create file inside directory in bash script

  17. 17

    Create a file in a directory using python

  18. 18

    Create a file in a directory using python

  19. 19

    How to create file in different directory

  20. 20

    How to create a .txt file using c#?

  21. 21

    How to read file index of a directory using C/C++?

  22. 22

    How to create a directory using Ansible

  23. 23

    How to create a temporary directory in C?

  24. 24

    Create Directory and Store File (C programming)

  25. 25

    Qt: How to create a directory in project file?

  26. 26

    File inside a directory script

  27. 27

    how can I create a file in the current user's home directory using Java?

  28. 28

    How to create a file example_user.rb in your application root directory in ROR using Sublime Text

  29. 29

    How can I create an XML file that is a list of files in a target directory using only Ant and XSLT?

HotTag

Archive