Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is my header file morse.h
- #ifndef GUARD
- #define GUARD
- #include <stdio.h>
- #include <string.h>
- #include <errno.h> //used to check for an error i encountered
- void initialize_morse_alphanum (char morseStrings[91][6]);
- void gather_input (char* inputFile, char* outputFile, char* mode);
- FILE* open_input_file (char* fileName);
- FILE* open_output_file (char* fileName);
- void english_to_morse(FILE* inputFile, FILE* outputFile, char morseStrongs[91][6]);
- #endif
- //this is my definitions.c file. it isn't really commented yet, and it is not all finished/ready for use yet
- #include "morse.h"
- void
- initialize_morse_alphanum (char morseStrings[91][6])
- { strcpy (morseStrings['0'], "-----");
- strcpy (morseStrings['1'], ".----");
- strcpy (morseStrings['2'], "..---");
- strcpy (morseStrings['3'], "...--");
- strcpy (morseStrings['4'], "....-");
- strcpy (morseStrings['5'], ".....");
- strcpy (morseStrings['6'], "-....");
- strcpy (morseStrings['7'], "--...");
- strcpy (morseStrings['8'], "---..");
- strcpy (morseStrings['9'], "----.");
- strcpy (morseStrings['A'], ".-");
- strcpy (morseStrings['B'], "-...");
- strcpy (morseStrings['C'], "-.-.");
- strcpy (morseStrings['D'], "-..");
- strcpy (morseStrings['E'], ".");
- strcpy (morseStrings['F'], "..-.");
- strcpy (morseStrings['G'], "--.");
- strcpy (morseStrings['H'], "....");
- strcpy (morseStrings['I'], "..");
- strcpy (morseStrings['J'], ".---");
- strcpy (morseStrings['K'], "-.-");
- strcpy (morseStrings['L'], ".-..");
- strcpy (morseStrings['M'], "--");
- strcpy (morseStrings['N'], "-.");
- strcpy (morseStrings['O'], "---");
- strcpy (morseStrings['P'], ".--.");
- strcpy (morseStrings['Q'], "--.-");
- strcpy (morseStrings['R'], ".-.");
- strcpy (morseStrings['S'], "...");
- strcpy (morseStrings['T'], "-");
- strcpy (morseStrings['U'], "..-");
- strcpy (morseStrings['V'], "...-");
- strcpy (morseStrings['W'], ".--");
- strcpy (morseStrings['X'], "-..-");
- strcpy (morseStrings['Y'], "-.--");
- strcpy (morseStrings['Z'], "--..");
- }
- void
- gather_input (char* inputFile, char* outputFile, char* mode)
- { puts ("Select the mode you would like to use. Enter...");
- puts (" ...-m for English-Morse conversion, or");
- printf (" ...-t for Morse-English conversion: ");
- scanf("%s", mode);
- if (mode[strlen(mode)] != '\0')
- mode[strlen(mode)] = '\0';
- printf ("Enter the name of the source file: ");
- scanf("%s", inputFile);
- if (inputFile[strlen(inputFile)] != '\0')
- inputFile[strlen(inputFile)] = '\0';
- printf ("Enter the name of the destination file: ");
- scanf("%s", outputFile);
- if (outputFile[strlen(outputFile)] != '\0')
- outputFile[strlen(outputFile)] = '\0';
- }
- FILE*
- open_input_file (char* fileName) //opens source file to be read
- { /*FILE* handle = NULL;
- handle = fopen(*fileName, "r");
- if (handle == NULL) //wtf do i do
- {
- }
- return handle;*/
- return fopen(fileName, "r");
- }
- FILE*
- open_output_file (char* fileName) //opens destination file to be written
- { FILE* handle = NULL;
- handle = fopen (*fileName, "w");
- return handle;
- }
- void
- english_to_morse(FILE* inputFile, FILE* outputFile, char morseStrings[91][6])
- { char convert = '\0';
- for (convert = fgetc(inputFile);
- convert != EOF;
- convert = fgetc(inputFile))
- { fputs(morseStrings[convert], outputFile);
- fputc(' ', outputFile);
- printf ("%s ", morseStrings[convert]);
- }
- }
- //here's main.c
- #include "morse.h"
- int
- main (void)
- { FILE* inFile = NULL; //input file
- FILE* outFile = NULL; //output file
- int countChars = 0; //number of characters converted
- char conversionOption[3] = {'\0'}, //english to morse, or vice versa
- infileName[32] = {'\0'}, outfileName[32] = {'\0'};
- char morseStrings[91][6] = {'\0'};
- initialize_morse_alphanum (morseStrings);
- gather_input (infileName, outfileName, conversionOption);
- printf ("%s\n", infileName);
- if (!(inFile = open_input_file(infileName)))
- { fprintf(stderr, "Error opening '%s': %s\n",
- infileName, strerror(errno));
- }
- outFile = open_output_file(outfileName);
- english_to_morse(inFile, outFile, morseStrings);
- fclose(inFile);
- fclose(outFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement