I am trying to create a file, the file name is will be taken from argument. Also the string that will be written is taken from argument! But the creat function returns -1. I could not find why? Any help appreciated!
C Code:
int main(int argc, char **argv)
{
int fdc;
int fdo;
char *wr;
char *rd;
if (argc >= 3)
wr = argv[2];
else
wr = "\0";
printf("wr = %s\n", wr);
if (argc >= 3)
{
fdc = creat(argv[1], O_RDWR);
printf("fdc = %d\n", fdc);
if (fdc != -1)
{
fdo = open(argv[1], O_RDWR);
printf("fdo = %d", fdo);
if (fdo != -1)
{
printf("a file created\n");
write (fdo, wr, strlen(wr));
printf("writed in file that created\n");
read (fdo, rd, strlen(wr));
printf("read done\n");
printf("rd = %s\n",rd);
}
close(fdo);
}
}
}
Command:
get_next_line % ./a.out test.txt 012345
Output:
wr = 012345
fdc = -1
The second argument for creat function should be a mode, not a file flag.
S_IRWXU 00700 user (file owner) has read, write and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission