Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. /*Group 2 Program 1
  2. Paul Macclean
  3. Mike Gorse
  4. Robert Breckenridge
  5.  
  6. CSC 460
  7. Language Translations
  8.  
  9. Program Description:
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15.  
  16. typedef enum {false,true} logical;
  17.  
  18.  
  19. char INFILE_EXTENSION[] = ".in";
  20.  
  21.  
  22. void appendExtension(char *FileName,char *DesiredExtension) {
  23. //Get a pointer to extension (Last occurence of '.')
  24. char* filename_extension = strrchr(FileName, '.');
  25.  
  26. //If no extension, OR if extension is **NOT** the expected infile extension:
  27. if (filename_extension == NULL) {
  28. //Append extension
  29. strcat(FileName, INFILE_EXTENSION);
  30. }
  31. }
  32.  
  33. int getInfile(FILE **InFile, char *FileName, logical PromptFile) {
  34. logical fileexists = false;
  35. logical quit = false;
  36.  
  37. do {
  38. if (PromptFile) {
  39. printf("Please enter an input file: ");
  40. scanf_s("%s", FileName, 100);
  41. }
  42.  
  43. //if input is 'q'
  44. if (tolower(FileName[0]) == 'q')
  45. quit = true;
  46. else {
  47. appendExtension(FileName, INFILE_EXTENSION);
  48.  
  49. //Open the file
  50. fopen_s(InFile, FileName, "r");
  51.  
  52. if (*InFile == NULL) {
  53. printf("That file does not exist!");
  54. fileexists = false;
  55. PromptFile = true;
  56. }
  57. else {
  58. fileexists = true;
  59. }
  60.  
  61. }
  62. } while (fileexists == false && quit == false);
  63.  
  64. return quit; //indicate whether the user quit or not
  65. }
  66.  
  67. int getOutfile(FILE** OutFile, char* FileName, logical PromptFile) {
  68. logical fileexists = false;
  69. logical quit = false;
  70.  
  71. do {
  72. if (PromptFile) {
  73. printf("Please enter an output file: ");
  74. scanf_s("%s", FileName, 100);
  75. }
  76.  
  77. //if input is 'q'
  78. if (tolower(FileName[0]) == 'q')
  79. quit = true;
  80. else {
  81. appendExtension(FileName, INFILE_EXTENSION);
  82.  
  83. //Open the file
  84. fopen_s(OutFile, FileName, "r");
  85.  
  86. if (*OutFile == NULL) {
  87. printf("That file does not exist!");
  88. fileexists = false;
  89. PromptFile = true;
  90. }
  91. else {
  92. fileexists = true;
  93. }
  94. }
  95. } while (fileexists == false && quit == false);
  96.  
  97. return quit; //indicate whether the user quit or not
  98. }
  99.  
  100.  
  101.  
  102.  
  103. int main(int argc, char *argv[]) {
  104. logical promptinfile = true;
  105. logical promptoutfile = true;
  106.  
  107. FILE *infile = NULL;
  108. char inputname[100] = { 0 };
  109. char outputname[100] = { 0 };
  110.  
  111.  
  112. if (argc > 1) {
  113. promptinfile = false;
  114. strcpy(inputname, argv[1]);
  115. }
  116.  
  117. if (argc > 2) {
  118. promptoutfile = false;
  119. strcpy(outputname, argv[2]);
  120. }
  121.  
  122.  
  123. logical userquit = getInfile(&infile, inputname, promptinfile);
  124.  
  125. if (userquit == true) {
  126. printf("Coward.\n");
  127. }
  128. else {
  129. //get output file thingo
  130.  
  131. //int userquit = getOutfile(&infile, inputname, promptinfile, "Please enter an input file: ");
  132. }
  133.  
  134.  
  135. if (infile != NULL) {
  136. fclose(infile);
  137. }
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement