Skip to content

Instantly share code, notes, and snippets.

@xyos
Created December 9, 2014 01:48
Show Gist options
  • Save xyos/2ecbc7f72549ad604208 to your computer and use it in GitHub Desktop.
Save xyos/2ecbc7f72549ad604208 to your computer and use it in GitHub Desktop.
./a.out test.txt
#include <iterator>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
int * size_of_matrix(std::istream& str) {
static int size[2];
int rows = 0;
int words = 0;
std::string row;
bool word(false);
std::for_each(std::istreambuf_iterator<char>(str),
std::istreambuf_iterator<char>(),
[&](unsigned char c) {
word = !std::isspace(c)
&& (word || ++words);
c == '\n' && ++rows; });
size[0] = rows; //filas
size[1] = words/rows; //columnas
return size;
}
int main(int argc, char* argv[])
{
std::string input_text;
std::ifstream inFile(argv[1]);
int * size = size_of_matrix(inFile);
std::cout << "Number of rows: " << size[0] << std::endl;
std::cout << "Number of columns: " << size[1] << std::endl;
std::getline(std::cin, input_text);
return 0;
}
1 2 3
4 5 6
7 8 9
10 11 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment