fork(3) download
  1. //GREP.CPP
  2. //COPYRIGHT EMILE J. KEITH 2014//
  3. #include <iostream>
  4. #include <string>
  5. #include <boost/filesystem.hpp>//boost a collection of C++ open source libraries that are cross-platform compatible
  6. #include <regex>
  7. #include <boost/regex.hpp>//Has more features than the standard library regex
  8. #include <fstream>
  9. #include <exception>
  10. #include <stdlib.h>
  11. #include <iomanip>
  12. using namespace std;
  13. using namespace std::regex_constants;
  14. using namespace boost::filesystem;
  15. int main(int argc, char *argv[])
  16. {
  17.  
  18. if(argc<3)//Since the program name is the first function and there has to be an expression and another search parameter, there must be atlest 3 arguments on call
  19. {
  20. cout<<"No search area was provided!\n"<<endl;
  21. return 0;
  22. }
  23. else
  24. {
  25. bool iterateAllWithExtension=false;
  26. string searchPath,destination, extension;
  27. const string lastArgument(argv[argc-1]);
  28. if(lastArgument[lastArgument.find_last_of("/\\")+1]=='*' && lastArgument.find_last_of("/\\")+2==lastArgument.find_last_of("."))//If the char at the position after the last '\' or '/' in the provided path is a '*', and the following char is the last '.', signifying the start of the extension
  29. {
  30. searchPath=lastArgument.substr(0,lastArgument.find_last_of("/\\"));
  31. destination=lastArgument.substr(lastArgument.find_last_of("/\\")+1,string::npos);
  32. extension=destination.substr(destination.find_last_of('.'), string::npos);
  33. iterateAllWithExtension=true;
  34. }
  35. if(iterateAllWithExtension)
  36. {
  37. path p(searchPath);//path class belonging to the boost library
  38. if(exists(p))
  39. {
  40. }
  41. else
  42. {
  43. cout<<"The file or directory provided does not exist\n";
  44. return 0;
  45. }
  46. }
  47. path p(argv[argc-1]);//This p is has a different scope than the previous, and it can be used as a name since the previous one is already out of scope
  48. if(exists(p) || exists(searchPath))//This needs to be fixed, because a .*.txt cannot exist
  49. {
  50. string fileName=p.filename().string();
  51. if(fileName.find(".")==0)//If the filename they provided begins with a fullstop, which you cannot do due to file naming restricitons, then you are trying to look for all files with a certain extension
  52. {
  53. }
  54. cout<<"Exists\n\n";
  55. if(is_regular_file(p))//function from the boost filesystem library
  56. {
  57. unsigned long endOfFile, currentPosition;
  58. ifstream file(argc[argv-1]);
  59. try
  60. {
  61. if(file.bad())
  62. {
  63. throw runtime_error("File is corrupted and cannot be opened for reading, program will now terminate\n");
  64. }
  65. else
  66. {
  67. file.seekg(0,ios::end);
  68. endOfFile=file.tellg();
  69. file.seekg(0,ios::beg);
  70. }
  71. }
  72. catch(runtime_error ex)//catches the runtime_error object thrown earlier
  73. {
  74. cerr<<ex.what()<<endl;
  75. exit(EXIT_FAILURE);//EXIT_FAILURE indicates that main must return 1 in windows, and whatever error terminating value in another operating system
  76. }
  77. //string sExpression(argv[argc-2]);
  78. const string toMatch=argv[argc-2];
  79. regex expression(toMatch);//the expression will be the second to last argument passed to main
  80. smatch match;//smatch is a typedef of match_results
  81. string currentLine;
  82. cout<<"In file "<<p.filename()<<" : \n";
  83. cout<<"__________________________________________"<<endl;
  84. unsigned short lineNumber=1;
  85. while(getline(file, currentLine))
  86. {
  87. currentPosition=file.tellg();
  88. if(currentPosition==endOfFile)
  89. {
  90. lineNumber=1;
  91. break;
  92. }
  93. else
  94. {
  95. if(regex_search(currentLine,expression))
  96. cout<<lineNumber<<"."<<currentLine<<"\n";
  97. }
  98. lineNumber++;
  99. }
  100. }
  101. else if(is_directory(p))
  102. {
  103. cout<<"It is a directory"<<endl;
  104. return 0;
  105. }
  106. }
  107. else
  108. {
  109. cout<<"The file or directory provided does not exist\n";
  110. return 0;
  111. }
  112. }
  113. /*typedef bool bl;//Just playing with typedef
  114. bl tom=(bl)(string(argv[1])=="emile");
  115. cout<<argv[1]<<"\t"<<tom<<endl;
  116. if(tom)
  117. std::cout<<"I knew it was you"<<endl;
  118. else
  119. std::cout<<"why"<<endl;
  120. if(argc>2)
  121. {
  122. if(string(argv[2])=="-I")
  123. cout<<"It works"<<endl;
  124. }*/
  125. return 0;
  126. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:116: fatal error: boost/filesystem.hpp: No such file or directory
 #include <boost/filesystem.hpp>//boost a collection of C++ open source libraries that are cross-platform compatible
                                                                                                                    ^
compilation terminated.
stdout
Standard output is empty