Advertisement
Guest User

Text file saving cpp with split

a guest
May 22nd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. // WriteFile.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <fstream>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace System;
  10. using namespace std;
  11.  
  12. template<typename T>
  13. vector<T>
  14. split(const T & str, const T & delimiters) {
  15.     vector<T> v;
  16.     T::size_type start = 0;
  17.     auto pos = str.find_first_of(delimiters, start);
  18.     while(pos != T::npos) {
  19.         if(pos != start) // ignore empty tokens
  20.             v.emplace_back(str, start, pos - start);
  21.         start = pos+1;
  22.         pos = str.find_first_of(delimiters, start);
  23.     }
  24.     if(start < str.length()) // ignore trailing delimiter
  25.         v.emplace_back(str, start, str.length() - start); // add what's left of the string
  26.     return v;
  27. }
  28.  
  29. int main(array<System::String ^> ^args)
  30. {
  31.     ifstream readFile("TEST.TXT");
  32.     string line;
  33.  
  34.     if (readFile) {
  35.         cout << "found file" << endl;
  36.         while (getline (readFile,line) ) {
  37.             vector<string> s = split<string>(line, ":");
  38.             cout << s[0] << endl << s[1] << endl;
  39.         }
  40.     } else {
  41.         cout << "unable to open text file" << endl;
  42.         ofstream out("TEST.TXT");
  43.         if (out) {
  44.         out << "LINE1:HELLO" << endl;
  45.         out << "LINE2:WORLD" << endl;
  46.         } else {
  47.             cout << "COULD NOT CREATE FILE" << endl;
  48.         }
  49.     }
  50.  
  51.     int i;
  52.     cin >> i;
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement