Skip to content

Instantly share code, notes, and snippets.

@bryangoodrich
Created July 25, 2015 00:12
Show Gist options
  • Save bryangoodrich/1acff2ea6b02aa62b1b6 to your computer and use it in GitHub Desktop.
Save bryangoodrich/1acff2ea6b02aa62b1b6 to your computer and use it in GitHub Desktop.
Input and rotate a grid through designated input format and file import
/*********************************************************************
* Compilation: g++ grid_file.cpp -o grid_file.exe
* Execution: grid_file.exe input.txt
*
* Input and rotate a grid
*
* This is a toy code snippet for handling informational
* input (integer size of grid), importing multiple
* columns, and exporting with formatted output. This
* example uses C++ approaches to file and string input
* processing as compared to the grid.cpp example that
* solely uses standard input.
*
* $ cat input.txt
* 3
* NW N NE
* W 0 E
* SW S SE
*
* $ grid_file.exe input.txt
* NW W SW
* N 0 S
* NE E SE
*********************************************************************/
#include <iostream> // std::cout, std::cin, std::right, std::endl
#include <string> // std::string
#include <iomanip> // std::setw
#include <fstream> // std::ifstream
#include <cstdlib> // atoi
#include <sstream> // std::stringstream
int main(int argc, char* argv[])
{
int N;
if (argc != 2) return 1;
std::ifstream in(argv[1]);
std::string line;
std::getline(in, line);
N = atoi(line.c_str());
std::string arr[N][N];
for(int i=0; std::getline(in, line); ++i)
{
std::stringstream ss(line);
std::string a, b, c;
ss >> a >> b >> c;
arr[i][0] = a;
arr[i][1] = b;
arr[i][2] = c;
}
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
std::cout <<
std::right <<
std::setw(2) <<
arr[j][i] << " ";
}
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment