Advertisement
Guest User

Misurka pidr.

a guest
May 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #define DEFAULT_BUFFER_SIZE 64
  5.  
  6.  
  7. using namespace std;
  8.  
  9. string getFilePathFromTheConsole() {
  10. char filePath[DEFAULT_BUFFER_SIZE];
  11.  
  12. printf("\nPlease, specify the path to the text file of your desire. \nPath: ");
  13. cin.getline(filePath, DEFAULT_BUFFER_SIZE);
  14.  
  15. return string(filePath);
  16. }
  17.  
  18.  
  19. template<typename Container>
  20. bool in_quote(const Container& cont, const string& s)
  21. {
  22. return search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
  23. }
  24.  
  25.  
  26.  
  27. string readFromTheFile(string &filePath) {
  28. FILE* file;
  29.  
  30. if ((file = fopen(filePath.c_str(), "r+")) == NULL) {
  31. cerr << "Unable to open the file for reading." << endl;
  32. return NULL;
  33. }
  34.  
  35. string result;
  36. char readChar;
  37.  
  38. while ((readChar = fgetc(file)) != EOF) {
  39. result += readChar;
  40. }
  41.  
  42. fclose(file);
  43.  
  44. return result;
  45. }
  46.  
  47.  
  48.  
  49. string cutfromcode(string &originalString) {
  50. string changedString;
  51. changedString = originalString;
  52.  
  53.  
  54. int stcount = changedString.find('0');
  55. while (stcount!=-1) {
  56. changedString.erase(stcount , 1);
  57. changedString.insert(stcount, "[zero]");
  58. stcount = changedString.find('0');
  59. }
  60.  
  61.  
  62. stcount = changedString.find('1');
  63. while (stcount != -1) {
  64. changedString.erase(stcount, 1);
  65. changedString.insert(stcount, "[one]");
  66. stcount = changedString.find('1');
  67. }
  68.  
  69.  
  70. stcount = changedString.find('2');
  71. while (stcount != -1) {
  72. changedString.erase(stcount, 1);
  73. changedString.insert(stcount, "[two]");
  74. stcount = changedString.find('2');
  75. }
  76.  
  77.  
  78. stcount = changedString.find('3');
  79. while (stcount != -1) {
  80. changedString.erase(stcount, 1);
  81. changedString.insert(stcount, "[tree]");
  82. stcount = changedString.find('3');
  83. }
  84.  
  85.  
  86. stcount = changedString.find('4');
  87. while (stcount != -1) {
  88. changedString.erase(stcount, 1);
  89. changedString.insert(stcount, "[four]");
  90. stcount = changedString.find('4');
  91. }
  92.  
  93.  
  94. stcount = changedString.find('5');
  95. while (stcount != -1) {
  96. changedString.erase(stcount, 1);
  97. changedString.insert(stcount, "[five]");
  98. stcount = changedString.find('5');
  99. }
  100.  
  101.  
  102. stcount = changedString.find('6');
  103. while (stcount != -1) {
  104. changedString.erase(stcount, 1);
  105. changedString.insert(stcount, "[six]");
  106. stcount = changedString.find('6');
  107. }
  108.  
  109.  
  110. stcount = changedString.find('7');
  111. while (stcount != -1) {
  112. changedString.erase(stcount, 1);
  113. changedString.insert(stcount, "[seven]");
  114. stcount = changedString.find('7');
  115. }
  116.  
  117.  
  118. stcount = changedString.find('8');
  119. while (stcount != -1) {
  120. changedString.erase(stcount, 1);
  121. changedString.insert(stcount, "[eight]");
  122. stcount = changedString.find('8');
  123. }
  124.  
  125.  
  126. stcount = changedString.find('9');
  127. while (stcount != -1) {
  128. changedString.erase(stcount, 1);
  129. changedString.insert(stcount, "[nine]");
  130. stcount = changedString.find('9');
  131. }
  132.  
  133. return changedString;
  134. }
  135.  
  136. bool writeIntoTheFile(string &filePath, string content, bool append) {
  137. FILE* file;
  138.  
  139. if ((file = fopen(filePath.c_str(), (append ? "a+" : "w+"))) == NULL) {
  140. cerr << "Unable to open the file for writing." << endl;
  141. return false;
  142. }
  143.  
  144. int charCount = strlen(content.c_str());
  145.  
  146. for (int i = 0; i < charCount; i++) {
  147. fputc(content.c_str()[i], file);
  148. }
  149.  
  150. fclose(file);
  151.  
  152. return true;
  153. }
  154.  
  155.  
  156. void main(){
  157.  
  158.  
  159. string filePath = getFilePathFromTheConsole();
  160. string readData = readFromTheFile(filePath);
  161. string uppercasedData = cutfromcode(readData);
  162.  
  163. writeIntoTheFile(filePath, uppercasedData, false);
  164.  
  165. printf("\n - Read Data -> %s \n - Edited Data -> %s", readData.c_str(), uppercasedData.c_str());
  166. printf("\n - Editing was done successfully.\n");
  167.  
  168. system("pause");
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement