Just to make more clear and readable your code Here are my observation :
First thing when you are using open() system call with O_CREAT flag, you should provide explicit permission, reason for this is if you observe man page of open() written by Michael Kerrisk, open() with 2 argument and open() with 3 arguments.
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
when you are creating file and writing/appending/truncating file using open() system call you should use open() call with 3 argument . as man page says
mode specifies the permissions to use in case a new file is created. This argument must be supplied when O_CREAT is specified
in flags and make sure umask value is not set to 0.
Refer the man pages for this "The effective permissions are modified by the process's umask in
the usual way";
and if you try to open a already existing file using open() you can use open() system call with 2 arguments as man page says "if O_CREAT is not specified, then mode is ignored"
So replace this
int filedesc = open("testfile.txt", O_WRONLY | O_APPEND | O_CREAT);
with
int filedesc = open("testfile.txt", O_WRONLY | O_APPEND | O_CREAT , 0664);
if (filedesc == -1) {
perror("open");
…exit or return or …
}
Second thing, somestr is array of char pointers, each pointer can have any length. Instead of random no of characters (24 as mentioned), if you want to make it generic, use strlen(somestr[i]).
int no_of_string = sizeof(somestr)/sizeof(somestr[0]);
for (int i = 0; i < no_of_string; ++i) {
write(filedesc, somestr[i], strlen(somestr[i]));
}
open()options includeO_CREAT, the function requires a third argument which are the permissions on the file to be created. An octal value like 0644 is appropriate. Historically, 0666 was used, leaving it toumaskandumask()to ensure that public write permission (and group write permission) are not given. Modern secure thinking avoids making things publicly writable even ifumaskis set to 0.