Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>;
- #include <fstream>;
- #include <string>;
- #include <regex>;
- using namespace std;
- int main() {
- string line;
- string s;
- ifstream myfile;
- ofstream myfile2;
- string data[2][2];
- vector<string> str_result;
- int a = 0;
- int b = 0;
- myfile.open("D:\\Dev\\Projects\\Project1\\assignment2.txt");
- if (myfile.is_open())
- {
- // read file
- cout << "Reading from File" << endl;
- while (getline(myfile, line))
- {
- cout << line << '\n';
- }
- myfile.close();
- // tokening with regex
- regex reg("\\s+");
- sregex_token_iterator iter(line.begin(), line.end(), reg, -1);
- sregex_token_iterator end;
- vector<string> vec(iter, end);
- // assigning value to array
- for (auto v : vec)
- {
- data[a][b] = v;
- b++;
- if (b > 1) {
- b = 0;
- a++;
- }
- }
- // first display
- cout << endl;
- cout << "First Display" << endl;
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 2; y++) {
- cout << data[x][y] << ' ';
- if (y == 1) {
- cout << endl;
- }
- }
- }
- myfile2.open("D:\\Dev\\Projects\\Project1\\swap.txt");
- // second display
- cout << endl;
- cout << "Second Display" << endl;
- for (int x = 0; x < 2; x++) {
- for (int y = 1; y >= 0; y--) {
- cout << data[x][y] << ' ';
- myfile2 << data[x][y] << ' ';
- if (y == 0) {
- cout << endl;
- myfile2 << endl;
- }
- }
- }
- myfile2.close();
- } else {
- cout << "Unable to open file\n";
- }
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement