Advertisement
Guest User

NWilli / Morse file

a guest
Nov 15th, 2010
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. // This is my header file morse.h
  2.  
  3. #ifndef GUARD
  4. #define GUARD
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <errno.h> //used to check for an error i encountered
  9.  
  10. void initialize_morse_alphanum (char morseStrings[91][6]);
  11. void gather_input (char* inputFile, char* outputFile, char* mode);
  12. FILE* open_input_file (char* fileName);
  13. FILE* open_output_file (char* fileName);
  14. void english_to_morse(FILE* inputFile, FILE* outputFile, char morseStrongs[91][6]);
  15.  
  16. #endif
  17.  
  18.  
  19. //this is my definitions.c file. it isn't really commented yet, and it is not all finished/ready for use yet
  20.  
  21. #include "morse.h"
  22.  
  23. void
  24. initialize_morse_alphanum (char morseStrings[91][6])
  25. {   strcpy (morseStrings['0'], "-----");
  26.     strcpy (morseStrings['1'], ".----");
  27.     strcpy (morseStrings['2'], "..---");
  28.     strcpy (morseStrings['3'], "...--");
  29.     strcpy (morseStrings['4'], "....-");
  30.     strcpy (morseStrings['5'], ".....");
  31.     strcpy (morseStrings['6'], "-....");
  32.     strcpy (morseStrings['7'], "--...");
  33.     strcpy (morseStrings['8'], "---..");
  34.     strcpy (morseStrings['9'], "----.");
  35.     strcpy (morseStrings['A'], ".-");
  36.     strcpy (morseStrings['B'], "-...");
  37.     strcpy (morseStrings['C'], "-.-.");
  38.     strcpy (morseStrings['D'], "-..");
  39.     strcpy (morseStrings['E'], ".");
  40.     strcpy (morseStrings['F'], "..-.");
  41.     strcpy (morseStrings['G'], "--.");
  42.     strcpy (morseStrings['H'], "....");
  43.     strcpy (morseStrings['I'], "..");
  44.     strcpy (morseStrings['J'], ".---");
  45.     strcpy (morseStrings['K'], "-.-");
  46.     strcpy (morseStrings['L'], ".-..");
  47.     strcpy (morseStrings['M'], "--");
  48.     strcpy (morseStrings['N'], "-.");
  49.     strcpy (morseStrings['O'], "---");
  50.     strcpy (morseStrings['P'], ".--.");
  51.     strcpy (morseStrings['Q'], "--.-");
  52.     strcpy (morseStrings['R'], ".-.");
  53.     strcpy (morseStrings['S'], "...");
  54.     strcpy (morseStrings['T'], "-");
  55.     strcpy (morseStrings['U'], "..-");
  56.     strcpy (morseStrings['V'], "...-");
  57.     strcpy (morseStrings['W'], ".--");
  58.     strcpy (morseStrings['X'], "-..-");
  59.     strcpy (morseStrings['Y'], "-.--");
  60.     strcpy (morseStrings['Z'], "--..");
  61. }
  62.  
  63. void
  64. gather_input (char* inputFile, char* outputFile, char* mode)
  65. {   puts ("Select the mode you would like to use. Enter...");
  66.     puts ("    ...-m for English-Morse conversion, or");
  67.     printf ("    ...-t for Morse-English conversion: ");
  68.     scanf("%s", mode);
  69.     if (mode[strlen(mode)] != '\0')
  70.         mode[strlen(mode)] = '\0';
  71.     printf ("Enter the name of the source file: ");
  72.     scanf("%s", inputFile);
  73.     if (inputFile[strlen(inputFile)] != '\0')
  74.         inputFile[strlen(inputFile)] = '\0';
  75.     printf ("Enter the name of the destination file: ");
  76.     scanf("%s", outputFile);
  77.     if (outputFile[strlen(outputFile)] != '\0')
  78.         outputFile[strlen(outputFile)] = '\0';
  79. }
  80.  
  81. FILE*
  82. open_input_file (char* fileName) //opens source file to be read
  83. {   /*FILE* handle = NULL;
  84.     handle = fopen(*fileName, "r");
  85.     if (handle == NULL) //wtf do i do
  86.         {
  87.         }
  88.     return handle;*/
  89.     return fopen(fileName, "r");
  90. }
  91.  
  92. FILE*
  93. open_output_file (char* fileName)   //opens destination file to be written
  94. {   FILE* handle = NULL;
  95.     handle = fopen (*fileName, "w");
  96.     return handle;
  97. }
  98.  
  99. void
  100. english_to_morse(FILE* inputFile, FILE* outputFile, char morseStrings[91][6])
  101. {   char convert = '\0';
  102.     for (convert = fgetc(inputFile);
  103.         convert != EOF;
  104.         convert = fgetc(inputFile))
  105.     {   fputs(morseStrings[convert], outputFile);
  106.         fputc(' ', outputFile);
  107.         printf ("%s ", morseStrings[convert]);
  108.     }
  109. }
  110.  
  111.  
  112.  
  113. //here's main.c
  114.  
  115. #include "morse.h"
  116.  
  117. int
  118. main (void)
  119. {   FILE* inFile = NULL;  //input file
  120.     FILE* outFile = NULL; //output file
  121.     int countChars = 0; //number of characters converted
  122.     char conversionOption[3] = {'\0'}, //english to morse, or vice versa
  123.         infileName[32] = {'\0'}, outfileName[32] = {'\0'};
  124.     char morseStrings[91][6] = {'\0'};
  125.  
  126.     initialize_morse_alphanum (morseStrings);
  127.     gather_input (infileName, outfileName, conversionOption);
  128.     printf ("%s\n", infileName);
  129.     if (!(inFile = open_input_file(infileName)))
  130.     {   fprintf(stderr, "Error opening '%s': %s\n",
  131.         infileName, strerror(errno));
  132.     }
  133.     outFile = open_output_file(outfileName);
  134.     english_to_morse(inFile, outFile, morseStrings);
  135.  
  136.     fclose(inFile);
  137.     fclose(outFile);
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement