Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <cstdlib>
  6. //#include <ostream>
  7. #include <cmath>
  8. #include <sys/stat.h>
  9.  
  10. using namespace std;
  11.  
  12. unsigned long GetFileSize(std::string filename)
  13. {
  14. struct stat stat_buf;
  15. int rc = stat(filename.c_str(), &stat_buf);
  16. return rc == 0 ? stat_buf.st_size : -1;
  17. }
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21. //define variables
  22. string filename1 = "output.txt";
  23. string filename2 = "output.vxl";
  24. bool wait = false;
  25.  
  26. //read command line parameters
  27. if (argc > 1 && string(argv[1]) != "0") {filename1 = string(argv[1]);}
  28. if (argc > 2 && string(argv[2]) != "0") {filename2 = string(argv[2]);}
  29. if (argc > 3 && string(argv[0]).compare("wait")) {wait = true;}
  30.  
  31. //show status
  32. std::cout << argv[0] << endl;
  33. cout << "input - " << filename1 << " output - " << filename2 << endl;
  34.  
  35. //read file into memory
  36. string filedata; unsigned long filesize;
  37.  
  38. ifstream inputfile;
  39. stringstream strStream;
  40.  
  41. inputfile.open(filename1.c_str());
  42. if ( !inputfile.is_open() ) //checks if file is open, closes with code 1 if not
  43. {cout << "File could not be opened." << endl; if (wait){cout << "Enter something to close." << endl; cin >> wait;}; return 1;}
  44.  
  45. strStream << inputfile.rdbuf();
  46. filedata = strStream.str();
  47. inputfile.close();
  48.  
  49. filesize = filedata.length();
  50. cout << filesize << " values read." << endl;
  51.  
  52. //parse data and save as binary
  53. unsigned long n;
  54. string currentdata = "";
  55. char delimiter;
  56. char value;
  57. float percent;
  58.  
  59. //find the delimiter
  60. for (n=0; n<5; n++)
  61. { if ( 47 > filedata.at(n) || filedata.at(n) > 58){delimiter = filedata.at(n);} }
  62. cout << "Delimter: " << delimiter << endl;
  63.  
  64. //check if file is open, closes with code 2 if not
  65. ofstream outputfile( filename2.c_str(), ios::out | ios::binary);
  66. if ( !outputfile.is_open() ) //checks if file is open, closes with code 1 if not
  67. {cout << "File could not be created." << endl; if (wait){cout << "Enter something to close." << endl; cin >> wait;}; return 1;}
  68.  
  69. //write values
  70. for (n=0; n<filesize; n++)
  71. {
  72. //if character is not delimiter and is not end of file then add to currentdata
  73. if ((filedata[n] != delimiter) && (n != filesize-1))
  74. {
  75. currentdata=currentdata+filedata[n];
  76. }
  77. //else write the value to file and reset currentdata
  78. else {
  79. value = atoi(currentdata.c_str());
  80. outputfile << value;
  81. currentdata = "";
  82. }
  83.  
  84. //updates the current iteration counter
  85. if (n % (filesize/20+1) == 0)
  86. {
  87. cout << "\r" << " " << "\r";
  88. cout << trunc((float(n) / filesize)*100) << "% - " << n << " out of " << filesize;
  89. }
  90. //if (n mod (trunc(length(buffer)/20)+1) = 0) then
  91.  
  92. }
  93.  
  94. cout << "\r" << " " << "\r";
  95. cout << trunc((float(n) / filesize)*100) << "% - " << n << " out of " << filesize << endl;
  96.  
  97. outputfile.close();
  98.  
  99. cout << "Writing finished: " << filename2 << " (" << trunc(GetFileSize(filename2)/float(10000))/float(100) << "MB)." << endl;
  100.  
  101. if (wait){cout << "Enter something to close." << endl; cin >> wait;}
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement