fork download
  1. #include <iostream>
  2. #include <ostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. // Enumerators
  9. enum META_DATA_CODE { S, A, P, I, O, M };
  10. enum META_DATA_DESCRIPTERS { end, hard, keyboard, printer, monitor, run, start, allocate, mouse, speaker };
  11. /*
  12. Note: "hard" in the descripter type will be for the hard drive. We shall not have any name conflictions
  13. or spacings between words
  14. */
  15.  
  16. // External Headers
  17. // #include "MemoryHandling"
  18. // #include "ErrorHandling"
  19.  
  20. // Public Functions
  21. bool is_conf( const std::string &path );
  22. bool isEmpty( std::ifstream &file );
  23.  
  24. int main( int argc, char* argv[] )
  25. {
  26. // Initialize Variables and Constants
  27.  
  28. // File Stream Management
  29. std::ifstream inFile;
  30. std::ofstream outFile;
  31. std::string STRING;
  32.  
  33. // Error check command line
  34. if( argc != 2 )
  35. {
  36. // This will automatically check for a missing file name
  37. std::cout << "Not enough arguments, please enter a file name" << std::endl;
  38. std::cout << "Program Terminated." << std::endl;
  39. }
  40.  
  41. // Error checking for file types, mispellings, or not found files
  42.  
  43. // Attempt to open file
  44. inFile.open( argv[1] );
  45.  
  46. // File has incorrect extension (e.g. .conf)
  47. // if( extension is NOT .conf and NOT .cnf )
  48. if( !is_conf( argv[1] ) )
  49. {
  50. // Display general error message and terminate program
  51. std::cout << "You have entered a file that does not have the .conf extension." << std::endl;
  52. std::cout << "Program Terminated" << std::endl;
  53. }
  54.  
  55. // Incorrect file name spelling
  56. // File is not found in the system
  57. else if(inFile.fail() )
  58. {
  59. std::cout << "ERROR: File could not be found. Please check spelling" << std::endl;
  60. std::cout << "Program Terminated." << std::endl;
  61. }
  62.  
  63. // File is empty
  64. else if( isEmpty( inFile ) )
  65. {
  66. std::cout << "ERROR: File is empty or corrupted!" << std::endl;
  67. std::cout << "Program Terminated." << std::endl;
  68. }
  69.  
  70. // File was correctly entered within specifications
  71. else
  72. {
  73.  
  74. std::cout << argv[1] << std::endl;
  75. std::cout << "File Successfully opened. Please wait." << std::endl;
  76.  
  77. while(!inFile.eof())
  78. {
  79. getline( inFile, STRING );
  80. std::cout << STRING << std::endl;
  81. }
  82. }
  83.  
  84.  
  85. return 0;
  86. }
  87.  
  88. // Function Implementations
  89.  
  90. // This function is to check within the file name that it has the matching
  91. // Extension of .conf before even reading the file.
  92. bool is_conf( const std::string &path )
  93. {
  94. std::string isConfExt = path.substr(path.length() - 5);
  95.  
  96. return isConfExt == ".conf";
  97. }
  98.  
  99. // This function checks if the file opened is empty
  100. // Terminate program if the file is empty or corrupted
  101. bool isEmpty( std::ifstream &file )
  102. {
  103. bool isempty = false;
  104.  
  105. file.seekg(0, std::ios::end);
  106. size_t size = file.tellg();
  107.  
  108. if( size == 0 )
  109. {
  110. isempty = true;
  111. }
  112.  
  113. return isempty;
  114. }
  115.  
  116.  
  117. /*
  118. Start Simulator Configuration File
  119. Version/Phase: 1.0
  120. File Path: test_1a.mdf
  121. Processor cycle time (msec): 10
  122. Monitor display time (msec): 25
  123. Hard drive cycle time (msec): 50
  124. Printer cycle time (msec): 500
  125. Keyboard cycle time (msec): 1000
  126. Memory cycle time (msec): 35
  127. Mouse cycle time (msec): 20
  128. Speaker cycle time (msec): 10
  129. Log: Log to Both
  130. Log File Path: output/output_1.out
  131. End Simulator Configuration File
  132. */
  133.  
  134.  
  135.  
  136.  
Runtime error #stdin #stdout #stderr 0s 3472KB
stdin
./a.out config_1.conf
stdout
Not enough arguments, please enter a file name
Program Terminated.
stderr
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid