Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. typedef unsigned int uint;
  2.  
  3.  
  4. inline void fileExists (const std::string& name) {
  5. if ( access( name.c_str(), F_OK ) == -1 ) {
  6. throw std::string("File does not exist!");
  7. }
  8. }
  9.  
  10. size_t bimNCols(std::string fn) {
  11. try {
  12. fileExists(fn);
  13. std::ifstream in_file(fn);
  14. std::string tmpline;
  15. std::getline(in_file, tmpline);
  16. std::vector<std::string> strs;
  17. strs = boost::split(strs, tmpline, boost::is_any_of("t"), boost::token_compress_on);
  18. return strs.size();
  19. } catch (const std::string& e) {
  20. std::cerr << "n" << e << "n";
  21. exit(EXIT_FAILURE);
  22. }
  23. }
  24.  
  25. typedef std::vector<std::string> vecStr;
  26.  
  27. vecStr bimReadCol(std::string fn, uint ncol_select) {
  28. try {
  29. size_t ncols = bimNCols(fn);
  30. if(ncol_select < 1 or ncol_select > ncols) {
  31. throw std::string("Your column selection is out of range!");
  32. }
  33.  
  34. std::ifstream in_file(fn);
  35. std::string tmpword;
  36. vecStr colsel; // holds the column of strings
  37. while (in_file) {
  38. for(int i=1; i<ncol_select; i++) {
  39. in_file >> tmpword;
  40. }
  41. in_file >> tmpword;
  42. colsel.push_back(tmpword);
  43. in_file.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
  44. }
  45. return colsel;
  46.  
  47. } catch (const std::string& e) {
  48. std::cerr << "n" << e << "n";
  49. exit(EXIT_FAILURE);
  50. }
  51. }
  52.  
  53. in_file.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
  54.  
  55. a 1 b 2
  56. a 1 b 2
  57. a 1 b 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement