Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <cstdlib>
- //#include <ostream>
- #include <cmath>
- #include <sys/stat.h>
- using namespace std;
- unsigned long GetFileSize(std::string filename)
- {
- struct stat stat_buf;
- int rc = stat(filename.c_str(), &stat_buf);
- return rc == 0 ? stat_buf.st_size : -1;
- }
- int main(int argc, char* argv[])
- {
- //define variables
- string filename1 = "output.txt";
- string filename2 = "output.vxl";
- bool wait = false;
- //read command line parameters
- if (argc > 1 && string(argv[1]) != "0") {filename1 = string(argv[1]);}
- if (argc > 2 && string(argv[2]) != "0") {filename2 = string(argv[2]);}
- if (argc > 3 && string(argv[0]).compare("wait")) {wait = true;}
- //show status
- std::cout << argv[0] << endl;
- cout << "input - " << filename1 << " output - " << filename2 << endl;
- //read file into memory
- string filedata; unsigned long filesize;
- ifstream inputfile;
- stringstream strStream;
- inputfile.open(filename1.c_str());
- if ( !inputfile.is_open() ) //checks if file is open, closes with code 1 if not
- {cout << "File could not be opened." << endl; if (wait){cout << "Enter something to close." << endl; cin >> wait;}; return 1;}
- strStream << inputfile.rdbuf();
- filedata = strStream.str();
- inputfile.close();
- filesize = filedata.length();
- cout << filesize << " values read." << endl;
- //parse data and save as binary
- unsigned long n;
- string currentdata = "";
- char delimiter;
- char value;
- float percent;
- //find the delimiter
- for (n=0; n<5; n++)
- { if ( 47 > filedata.at(n) || filedata.at(n) > 58){delimiter = filedata.at(n);} }
- cout << "Delimter: " << delimiter << endl;
- //check if file is open, closes with code 2 if not
- ofstream outputfile( filename2.c_str(), ios::out | ios::binary);
- if ( !outputfile.is_open() ) //checks if file is open, closes with code 1 if not
- {cout << "File could not be created." << endl; if (wait){cout << "Enter something to close." << endl; cin >> wait;}; return 1;}
- //write values
- for (n=0; n<filesize; n++)
- {
- //if character is not delimiter and is not end of file then add to currentdata
- if ((filedata[n] != delimiter) && (n != filesize-1))
- {
- currentdata=currentdata+filedata[n];
- }
- //else write the value to file and reset currentdata
- else {
- value = atoi(currentdata.c_str());
- outputfile << value;
- currentdata = "";
- }
- //updates the current iteration counter
- if (n % (filesize/20+1) == 0)
- {
- cout << "\r" << " " << "\r";
- cout << trunc((float(n) / filesize)*100) << "% - " << n << " out of " << filesize;
- }
- //if (n mod (trunc(length(buffer)/20)+1) = 0) then
- }
- cout << "\r" << " " << "\r";
- cout << trunc((float(n) / filesize)*100) << "% - " << n << " out of " << filesize << endl;
- outputfile.close();
- cout << "Writing finished: " << filename2 << " (" << trunc(GetFileSize(filename2)/float(10000))/float(100) << "MB)." << endl;
- if (wait){cout << "Enter something to close." << endl; cin >> wait;}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement