/*
3. Repeat part 2 as per the following procedure:
(a) Read the next 100 characters from source.tx
Among characters read, replace each character '1' with character 'A'
All characters are then written in destination.txt
(b) Write characters "XYZ" into file destination.txt
(c) Repeat the previous steps until the end of file source.txt.
The last read step my not have 100 characters
SYSTEM CALLS TO USE:
read(): ssize_t read(int fildes, void *buf, size_t nbytes)
int fildes: the file descriptor of where to read the input (in this program it is obtained from the open() system call)
const void *buf: a character array where the read content will be stored
size_t nbytes: the number of bytes to read before truncating the data
returns the number of bytes that were read (in ssize_t type).
A return value of 0 indicates EOF
If an error occurs, read() returns -1
write(): data is written to a file
Prototype: ssize_t write(int fd, const void *buf, size_t nbytes);
fd: file descriptor to write to
buf: points to a character array which can be used to store the content obtained from the file pointed to by fd
nbytes: specifies the number of bytes to be written from the character array into the file pointed to by fd
ssize_t is a signed data type.
Returns the number of bytes successfully written into the array, which may be less than the specified nbytes
Returns -1 if an error occurs.
=====================================================================
PROGRAM: cp-mimic-char-manipulation.c
FORM OF USAGE: ./cp-mimic-char-manipulation.c source destination
VALUES TO BE RETURNED:
0: if the program was executed successfuly
-1: if an error was encountered
=====================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h> /* API for open() */
#include <sys/stat.h> /* API for open() */
#include <fcntl.h> /* API for open() */
#include <unistd.h> /* API for read() and write()
Defines the ssize_t and size_t data types (return values for read())*/
char buffer[BUFSIZ]; /* Buffer to manipulate data */
int main(int argc, char *argv[])
{
int file_source; /* File descriptor to be read */
int file_destination; /* File descriptor to write to */
int nbytes;
ssize_t nr;
const char *text = "XYZ"; /* To be written in destination file at the end of each line */
/* Analysis of the arguments of the command line */
if (argc != 3) {
fprintf(stderr
,"Please follow the structure: %s source destination\n", argv
[0]); exit(-1); /* error was encountered */ }
/* Opening the source file in read-only mode (O_RDONLY). */
file_source = open(argv[1], O_RDONLY);
if (file_source == -1) { /* if error was encountered */
fprintf(stderr
, "Error opening source file \n"); }
/* Opening or creating a destination file in write-only mode:
(i) Open file in write-only mode (O_WRONLY)
(ii) If the file exists then trunc to zero bytes (O_TRUNC)
(iii) The file is created in the case that it doesn't exist (O_CREAT)
(iv) The mode to be created is 0666 */
file_destination = open(argv[2], O_WRONLY|O_TRUNC|O_CREAT, 0666);
if (file_destination == -1) { /* if error was encountered */
fprintf(stderr
, "Cannot open destination file "); }
/* Copying the source file to the destination file*/
/*Reading only 100 characters at a time */
while ((nbytes = read(file_source, buffer, 100)) > 0){
int i = 0;
while (i < nbytes){
if (buffer[i] == '1')
buffer[i] == 'A';
i++;
}
write(file_destination, buffer, nbytes);
write
(file_destination
, text
, strlen (text
)); } /* end of while-loop */
close(file_source);
close(file_destination);
} /* end of main */