Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- FILE *openFile(char filename[]);
- FILE *createFile(char filename[]);
- void printUsage();
- int main(int argc, char *argv[])
- {
- FILE *source; /* source file */
- FILE *dist; /* destination file */
- char arr[500]; /* a place to store lines from files */
- int i=0;
- /* if wrong number of arguments passed */
- if (argc != 3)
- printUsage(argv[0]);
- else {
- source = openFile(argv[1]);
- dist = createFile(argv[2]);
- /* scan each line and apply the cipher */
- while ((fgets(arr, 500, source)) != NULL) {
- for (i = 0; i < strlen(arr); i++)
- if (isalpha(arr[i]) && islower(arr[i]))
- arr[i] = (arr[i] + 13) > 'z' ? arr[i] - 13 : arr[i] + 13;
- else if (isalpha(arr[i]) && isupper(arr[i]))
- arr[i] = (arr[i] + 13) > 'Z' ? arr[i] - 13 : arr[i] + 13;
- fputs(arr, dist);
- }
- }
- fclose(source);
- fclose(dist);
- return EXIT_SUCCESS;
- }
- void printUsage(char programName[])
- {
- printf("USAGE: %s <filename1.txt> <filename2.txt>\n\n", programName);
- exit(1);
- }
- FILE *openFile(char filename[]){
- FILE *fp;
- fp = fopen(filename, "r");
- if (fp == NULL) {
- printf("\nERROR: file \"%s\" does not exist.\n", filename);
- exit(1);
- }
- else
- return fp;
- }
- FILE *createFile(char filename[]){
- FILE *fp;
- char response;
- /* check to see if distination file already exists */
- fp = fopen(filename, "r");
- if (fp != NULL) {
- while(1) {
- printf("Destination file already exits;");
- printf(" overwrite? [y/n] ");
- scanf("%c", &response);
- if ((tolower(response)) == 'y'){
- fclose(fp);
- fp = fopen(filename, "w");
- return fp;
- }
- else if ((tolower(response)) == 'n')
- exit(1);
- else {
- printf("invalid response.\n");
- scanf("%c", &response);
- /* this line picks up the '\n' so it doesn't
- get re-scanned in the next loop */
- }
- }
- }
- /* if file doesn't exist, create it */
- else {
- fp = fopen(filename, "w");
- if (fp == NULL) {
- printf("Unable to create destination file. make");
- printf(" sure you have proper permissions.\n");
- exit(1);
- }
- else
- return fp;
- }
- }
- // 'a' + (c-'a')%('z'-'a'+1)
Add Comment
Please, Sign In to add comment