Skip to content

Commit 9a35397

Browse files
committed
task 1
1 parent bde354a commit 9a35397

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

0x15-file_io/1-create_file.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "main.h"
2+
3+
/**
4+
* create_file - creates a file
5+
* @filename: filename to grab
6+
* @text_content: content to add
7+
* Return: int value
8+
*/
9+
int create_file(const char *filename, char *text_content)
10+
{
11+
int fd, len = 0;
12+
long int wrote;
13+
14+
if (filename == NULL)
15+
return (-1);
16+
fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600);
17+
if (fd == -1)
18+
return (-1);
19+
if (text_content != NULL)
20+
{
21+
22+
while (text_content[len])
23+
len++;
24+
wrote = write(fd, text_content, len);
25+
if (wrote == -1)
26+
return (-1);
27+
}
28+
if (close(fd) == -1)
29+
return (-1);
30+
return (1);
31+
}

0 commit comments

Comments
 (0)