Functional Requirements Write a program that opens a file for reading, and write
ID: 3728467 • Letter: F
Question
Functional Requirements
Write a program that opens a file for reading, and writes its contents to an output file, but the alphabetic ASCII characters have their case transposed: uppercase letters are copied as lowercase, and vice versa. Any other byte values are simply copied. The command expects 3 command line arguments: the pathname of the source file, the pathname of the destination file, and the permissions to be set on the destination file, expressed as 3 octal digits. If the target cannot be created (it already exists, or you lack permissions on that directory) display an error message and end.
ale: ./a.out source.txt target.txt 755
Non-functional Requirements
You may only use basic POSIX I/O and C libraries. Use a buffer size of 64 bytes for both read and write operations. Be sure to test/demonstrate your program an a text file bigger than 64 bytes in size.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
if(argc!=4){
printf("Invalid runtime arguments format ./a.out source.txt target.txt mode ");
exit(1);
}
char buffer[64];
int in, out;
in = open(argv[1], O_RDONLY);
if(in==-1){
printf("Error opening input file ");
exit(1);
}
int bytes = read(in, buffer, sizeof(buffer));
out = open(argv[2], O_CREAT | O_WRONLY, atoi(argv[3]));
if(out==-1){
printf("Error to create file ");
exit(1);
}
int i=0;
while(buffer[i]!=''){
if(buffer[i]>='A' && buffer[i]<='Z'){
buffer[i] = buffer[i]+32;
}
else if(buffer[i]>='a' && buffer[i]<='z'){
buffer[i] = buffer[i]-32;
}
i++;
}
write(out, buffer, bytes);
printf("Successfully wrote data ");
close(out);
close(in);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.