Write a function that reads a text file and prints it to the POSIX
standard output.
- Prototype:
ssize_t read_textfile(const char *filename, size_t letters);
- Where letters is the number of letters it should read and print
- Returns the actual number of letters it could read and print
- If the file can not be opened or read, return
0
- If
filename
isNULL
return0
- If
write
fails or does not write the expected amount of bytes, return0
Create a function that creates a file.
- Prototype:
int create_file(const char *filename, char *text_content);
- Where
filename
is the name of the file to create andtext_content
is aNULL
terminated string to write to the file - Returns:
1
on success,-1
on failure (file can not be created, file can not be written,write
“fails”, etc…) - The created file must have those permissions:
rw-------
. If the file already exists, do not change the permissions. - If the file already exists, truncate it
- If
filename
isNULL
return-1
- If
text_content
isNULL
create an empty file
Write a function that appends text at the end of a file.
- Prototype:
int append_text_to_file(const char *filename, char *text_content);
- Where
filename
is the name of the file andtext_content
is theNULL
terminated string to add at the end of the file - Return:
1
on success and-1
on failure - Do not create the file if it does not exist
- If
filename
isNULL
return-1
- If
text_content
isNULL
, do not add anything to the file. Return1
if the file exists and-1
if the file does not exist or if you do not have the required permissions to write the files
Write a program that copies the content of a file to another file.
- Usage:
cp file_from file_to
- If the number of argument is not the correct one, exit with code
97
and printUsage: cp file_from file_to
, followed by a new line, on thePOSIX
standard error - If
file_to
already exists, truncate it - If
file_from
does not exist, or if you can not read it, exit with code98
and printError: Can't read from file NAME_OF_THE_FILE
, followed by a new line, on thePOSIX
standard error- Where
NAME_OF_THE_FILE
is the first argument passed to your program
- Where
- If you can not create or if
write
tofile_to
fails, exit with code99
and printError: Can't write to NAME_OF_THE_FILE
, followed by a new line, on thePOSIX
standard error- Where
NAME_OF_THE_FILE
is the second argument passed to your program
- Where
- If you can not close a file descriptor, exit with code
100
and printError: Can't close fd FD_VALUE
, followed by a new line, on thePOSIX
standard error- Where
FD_VALUE
is the value of the file descriptor
- Where
- Permissions of the created file:
rw-rw-r--
. If the file already exists, do not change the permissions - You must read
1,024
bytes at a time from thefile_from
to make less system calls. Use a buffer - You are allowed to use
dprintf