-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-create_file.c
48 lines (43 loc) · 972 Bytes
/
1-create_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "holberton.h"
/**
* _strlen - gets string length
* @text: character pointer to string
* Return: size of string
*/
size_t _strlen(char *text)
{
size_t i;
for (i = 0; text[i]; i++)
;
return (i);
}
/**
* create_file - creates a file addes content to it
* @filename: name of the file (first argument)
* @text_content: content of file (second argument)
* Return: 1 if successful else -1
*/
int create_file(const char *filename, char *text_content)
{
int file, check;
if (filename == NULL)
return (-1);
/*if text_content is null create empty file*/
if (text_content == NULL)
{
text_content = "";
}
/*create or Truncate and set privilates to rw for owner*/
file = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (file == -1)
return (-1);
/*write to file, the content , content length*/
check = write(file, text_content, _strlen(text_content));
if (check == -1)
{
close(file);
return (-1);
}
close(file);
return (1);
}