Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <locale.h>
- using namespace std;
- char d, n;
- vector<std::string> split(const string& s, char delim)
- {
- vector<string> elems;
- stringstream ss(s);
- string item;
- while (getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
- }
- void TXTtoBIN(ifstream& in, ofstream& out)
- {
- string s;
- float num;
- double dbl;
- if (in.is_open() && out.is_open()) {
- while (!in.eof() && getline(in, s)){
- vector<string> elems = split(s, ' ');
- for (int i = 0; i < elems.size(); i++) {
- string elem = elems.at(i);
- if (elem.empty()) continue;
- else {
- switch (n)
- {
- case '1' :
- num = stof(elem);
- out.write(reinterpret_cast<char*>(&num), 4);
- break;
- case '2' :
- dbl = stof(elem);
- out.write(reinterpret_cast<char*>(&dbl), 8);
- break;
- }
- }
- }
- switch (n)
- {
- case '1':
- num = NAN;
- out.write(reinterpret_cast<char*>(&num), 4);
- break;
- case '2':
- dbl = NAN;
- out.write(reinterpret_cast<char*>(&dbl), 8);
- break;
- }
- }
- }
- }
- void BINtoTXT(ifstream& in, ofstream& out)
- {
- string s;
- float num;
- double dbl;
- if (in.is_open() && out.is_open()) {
- switch (n)
- {
- case '1':
- while ( !in.eof() )
- {
- in.read(reinterpret_cast<char*>(&num), 4);
- if (isnan(num)) {
- out << endl; continue;
- }
- out << num << " ";
- }
- break;
- case '2':
- while (in.read(reinterpret_cast<char*>(&dbl), 8)) {
- if (isnan(dbl)) {
- out << endl; continue;
- }
- out << dbl << " ";
- }
- break;
- }
- }
- }
- int main(int argc, char* argv[])
- {
- setlocale(LC_ALL, "RUS");
- if (argc < 5)
- {
- cout << "не заданы все аргументы" << endl;
- getchar();
- return 0;
- }
- string src = argv[1], dst = src.substr(0, src.find_last_of(".") + 1);
- int method = 0;
- if (src.substr(src.find_last_of(".") + 1) == "txt") {
- if (argv[4] == "-b")
- {
- cout << "Исходный файл .txt, но выставлен аргумент -b"<< endl;
- getchar();
- return 0;
- }
- method = 1;
- dst += "bin";
- }
- else if (src.substr(src.find_last_of(".") + 1) == "bin") {
- if (argv[4] == "-t")
- {
- cout << "Исходный файл .bin, но выставлен аргумент -t" << endl;
- getchar();
- return 0;
- }
- method = 2;
- dst += "txt";
- }
- else
- {
- cout << "неверное расширение исходного файла" << endl;
- getchar();
- return 0;
- }
- string param1 = argv[2];
- string param2 = argv[3];
- if (param1.substr(0, param1.find_last_of("=") + 1) == "-d=") {
- d = param1[param1.length() - 1];
- }
- else if (param1.substr(0, param1.find_last_of("=") + 1) == "-n="){
- n = param1[param1.length() - 1];
- }
- else {
- cout << "неверно заданы аргументы" << endl;
- getchar();
- return 0;
- }
- if (param2.substr(0, param2.find_last_of("=") + 1) == "-d="){
- d = param2[param2.length() - 1];
- }
- else if (param2.substr(0, param2.find_last_of("=") + 1) == "-n="){
- n = param2[param2.length() - 1];
- }
- else {
- cout << "неверно заданы аргументы" << endl;
- getchar();
- return 0;
- }
- ifstream fin;
- ofstream fout;
- switch (method)
- {
- case 1:
- fin.open(src);
- fout.open(dst, ios_base::binary);
- TXTtoBIN(fin, fout);
- break;
- case 2:
- fin.open(src, ios_base::binary);
- fout.open(dst);
- BINtoTXT(fin, fout);
- break;
- }
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement