/******************************************************* * Section : 15 * Machine No. : N * Roll No. : 19CS100XY * Name : Aritra Hazra * Assignment No : 9 * Description : File Handling and Comman-line Arguments *******************************************************/ #include #include #include #include // for srand(), rand() #include // for getpid() #include // for getpid() #define defaultOutputFile "output.txt" #define defaultInterFile "inter.txt" void printUsage() { fprintf(stderr,"USAGE:: ./a.out -input [-output ] [-flag ]\n\t([ ] indicates optional arguments)\n"); } int main(int argc, char **argv) { char *inputFile, *outputFile = defaultOutputFile, *flagOption = NULL; FILE *inputFP, *outputFP, *interFP; int i, shift, reportAlpha = 0, reportNum = 0, reportSymb = 0; char c; srand( getpid() ); // Opening the Read-Write Files From Command-Line Input if(argc > 7) { fprintf(stderr,"Error: Too Many Arguments!!\n"); printUsage(); exit(0); } if(argc < 3) { fprintf(stderr,"Error: Too Few Arguments!!\n"); printUsage(); exit(0); } for(i = 1; i < argc; i = i + 2) { if(!strcmp(argv[i],"-input")) { if(i + 1 == argc || !strcmp(argv[i + 1],"-output") || !strcmp(argv[i + 1],"-flag")) { fprintf(stderr,"Error: Input_File_Name Not Specified!!\n"); printUsage(); exit(0); } else { inputFile = argv[i + 1]; } } else { if(!strcmp(argv[i],"-output")) { if(i + 1 == argc || !strcmp(argv[i + 1],"-input") || !strcmp(argv[i + 1],"-flag")) { fprintf(stderr,"Error: Output_File_Name Not Specified!!\n"); printUsage(); exit(0); } else { outputFile = argv[i + 1]; } } else { if(!strcmp(argv[i],"-flag")) if(i + 1 == argc || !strcmp(argv[i + 1],"-input") || !strcmp(argv[i + 1],"-output")) { fprintf(stderr,"Error: Flag_Options Not Specified!!\n"); printUsage(); exit(0); } else { flagOption = argv[i + 1]; } else { fprintf(stderr,"Error: Wrong Arguments!!\n"); printUsage(); exit(0); } } } } // Check whether input file specified if(inputFile == NULL) { fprintf(stderr,"Error: Input_File_Name Not Specified!!\n"); printUsage(); exit(0); } else { inputFP = fopen(inputFile, "r"); if(inputFP == NULL) { // Checking input file error fprintf(stderr,"Error: Cannot Open Input File!!\n"); exit(0); } interFP = fopen(defaultInterFile, "w"); if(interFP == NULL) { // Checking intermediate file error fprintf(stderr,"Error: Cannot Open Intermediate File!!\n"); exit(0); } //making the intermediate file with caeser cipher text (alphabets shifted within [1-10]) shift = (char)(rand() % 10 + 1) ; // Generating shift within 1-10 range printf("shift = %d\n", shift); while((c = fgetc(inputFP)) != EOF) { if(isalpha(c)) { if(isupper(c)) fputc((c-'A'+shift)%26+'A', interFP); else fputc((c-'a'+shift)%26+'a', interFP); } else fputc(c, interFP); } fclose(interFP); interFP = fopen(defaultInterFile, "r"); if(interFP == NULL) { // checking intermediate file error fprintf(stderr,"Error: Cannot Open Intermediate File!!\n"); exit(0); } outputFP = fopen(outputFile, "w"); if(outputFP == NULL) { // Checking output file error fprintf(stderr,"Error: Cannot Open Output File!!\n"); exit(0); } if(flagOption == NULL) { // Checking flag option error fprintf(stderr,"Error: Cannot Find Flag Option!!\n"); exit(0); } for(i=0; flagOption[i] != '\0'; i++) { if(flagOption[i] == 'a') reportAlpha = 1; // need to report all alphabatic characters if(flagOption[i] == 'n') reportNum = 1; // need to report all numeric characters if(flagOption[i] == 's') reportSymb = 1; // need to report all symbols } // Reporting all alphabatic characters fputs("Alphabetic Characters:\n", outputFP); while((c = fgetc(interFP)) != EOF) { if(reportAlpha) { if(isalpha(c)) { fputc(c, outputFP); fputc(' ', outputFP); } } } fputc('\n', outputFP); // Reporting all numeric characters fseek(interFP, 0, SEEK_SET); fputs("Numeric Characters:\n", outputFP); while((c = fgetc(interFP)) != EOF) { if(reportNum) { if(isdigit(c)) { fputc(c, outputFP); fputc(' ', outputFP); } } } fputc('\n', outputFP); // Reporting all symbols (ignoring space) fseek(interFP, 0, SEEK_SET); fputs("Symbolic Characters:\n", outputFP); while((c = fgetc(interFP)) != EOF) { if(reportSymb) { if(!isalnum(c) && c != ' ') { if(c == '\n') fputs("\\n ", outputFP); else if(c == '\t') fputs("\\t ", outputFP); else { fputc(c, outputFP); fputc(' ', outputFP); } } } } fputs("\n\n", outputFP); fclose(interFP); fclose(inputFP); fclose(outputFP); } return 0; }