Advertisement
vlad_davydov

ready

Jul 13th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <locale.h>
  8.  
  9. using namespace std;
  10.  
  11. char d, n;
  12.  
  13. vector<std::string> split(const string& s, char delim)
  14. {
  15. vector<string> elems;
  16. stringstream ss(s);
  17. string item;
  18. while (getline(ss, item, delim)) {
  19. elems.push_back(item);
  20. }
  21. return elems;
  22. }
  23.  
  24. void TXTtoBIN(ifstream& in, ofstream& out)
  25. {
  26. string s;
  27. float num;
  28. double dbl;
  29. if (in.is_open() && out.is_open()) {
  30. while (!in.eof() && getline(in, s)){
  31. vector<string> elems = split(s, ' ');
  32. for (int i = 0; i < elems.size(); i++) {
  33. string elem = elems.at(i);
  34.  
  35. if (elem.empty()) continue;
  36.  
  37. else {
  38. switch (n)
  39. {
  40. case '1' :
  41. num = stof(elem);
  42. out.write(reinterpret_cast<char*>(&num), 4);
  43. break;
  44. case '2' :
  45. dbl = stof(elem);
  46. out.write(reinterpret_cast<char*>(&dbl), 8);
  47. break;
  48. }
  49. }
  50. }
  51.  
  52. switch (n)
  53. {
  54. case '1':
  55. num = NAN;
  56. out.write(reinterpret_cast<char*>(&num), 4);
  57. break;
  58. case '2':
  59. dbl = NAN;
  60. out.write(reinterpret_cast<char*>(&dbl), 8);
  61. break;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. void BINtoTXT(ifstream& in, ofstream& out)
  68. {
  69. string s;
  70. float num;
  71. double dbl;
  72.  
  73.  
  74. if (in.is_open() && out.is_open()) {
  75. switch (n)
  76. {
  77. case '1':
  78. while ( !in.eof() )
  79. {
  80. in.read(reinterpret_cast<char*>(&num), 4);
  81. if (isnan(num)) {
  82. out << endl; continue;
  83. }
  84. out << num << " ";
  85. }
  86. break;
  87. case '2':
  88. while (in.read(reinterpret_cast<char*>(&dbl), 8)) {
  89. if (isnan(dbl)) {
  90. out << endl; continue;
  91. }
  92. out << dbl << " ";
  93. }
  94. break;
  95. }
  96. }
  97. }
  98.  
  99.  
  100. int main(int argc, char* argv[])
  101. {
  102. setlocale(LC_ALL, "RUS");
  103.  
  104.  
  105. if (argc < 5)
  106. {
  107. cout << "не заданы все аргументы" << endl;
  108. getchar();
  109. return 0;
  110. }
  111.  
  112. string src = argv[1], dst = src.substr(0, src.find_last_of(".") + 1);
  113.  
  114. int method = 0;
  115. if (src.substr(src.find_last_of(".") + 1) == "txt") {
  116. if (argv[4] == "-b")
  117. {
  118. cout << "Исходный файл .txt, но выставлен аргумент -b"<< endl;
  119. getchar();
  120. return 0;
  121. }
  122. method = 1;
  123. dst += "bin";
  124. }
  125. else if (src.substr(src.find_last_of(".") + 1) == "bin") {
  126. if (argv[4] == "-t")
  127. {
  128. cout << "Исходный файл .bin, но выставлен аргумент -t" << endl;
  129. getchar();
  130. return 0;
  131. }
  132. method = 2;
  133. dst += "txt";
  134. }
  135. else
  136. {
  137. cout << "неверное расширение исходного файла" << endl;
  138. getchar();
  139. return 0;
  140. }
  141.  
  142. string param1 = argv[2];
  143. string param2 = argv[3];
  144.  
  145. if (param1.substr(0, param1.find_last_of("=") + 1) == "-d=") {
  146. d = param1[param1.length() - 1];
  147. }
  148. else if (param1.substr(0, param1.find_last_of("=") + 1) == "-n="){
  149. n = param1[param1.length() - 1];
  150. }
  151. else {
  152. cout << "неверно заданы аргументы" << endl;
  153. getchar();
  154. return 0;
  155. }
  156.  
  157.  
  158. if (param2.substr(0, param2.find_last_of("=") + 1) == "-d="){
  159. d = param2[param2.length() - 1];
  160. }
  161. else if (param2.substr(0, param2.find_last_of("=") + 1) == "-n="){
  162. n = param2[param2.length() - 1];
  163. }
  164. else {
  165. cout << "неверно заданы аргументы" << endl;
  166. getchar();
  167. return 0;
  168. }
  169.  
  170. ifstream fin;
  171. ofstream fout;
  172. switch (method)
  173. {
  174. case 1:
  175. fin.open(src);
  176. fout.open(dst, ios_base::binary);
  177. TXTtoBIN(fin, fout);
  178. break;
  179. case 2:
  180. fin.open(src, ios_base::binary);
  181. fout.open(dst);
  182. BINtoTXT(fin, fout);
  183.  
  184. break;
  185. }
  186.  
  187. fin.close();
  188. fout.close();
  189.  
  190. return 0;
  191.  
  192.  
  193. }
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement