감동, 마음이 움직이는 것

[c++] count # of row and col in data file 본문

Tips (Utility, Computer Language, and etc.)

[c++] count # of row and col in data file

Struggler J. 2018. 1. 4. 21:13

//Read file and gives the number of rows and columns

//<sstream> is needed for istrigstrem


#include <string>

#include <iostream>

#include <sstream>

#include <fstream>

using namespace std;


void Count_row_col(char* fname, int* row, int* col)

{

int N = 0;

ifstream in(fname);

string unused;

string s;

while ( getline(in, unused) )

{

if(N==1) { s=unused; }

++N;

}

*row = N;

istringstream iss(s);

int columns = 0;

while(iss) 

{

string subs; 

iss >> subs; 

if(subs.length()) 

{

++columns;

}

}

*col = columns;

cout << *row << " " << *col << endl;

}