I'm using CodeBlocks.
My file is like this:
A: B w w V
B: C w A W
C: D w B X
D: E w C Y
E: F w D Z
And i need to get the number of rows and columns, in this case is 4x5. (Letters with :
don't count)
My first solution is use a variable counter
an count the dimension of the matrix, but this is not a good solution.
What function can get the size of matrix in a .txt file? Please, any advice, suggestion, is welcome.
You need to read the file line by line using getline
std::vector<string> lines;
std::string line;
while(std::getlin(fileStream, line)
{
lines.push_back(line);
}
Now you have
int rowCount = lines.size();
Now for each of the lines you can count the number of elements in it using a string stream;
std::istringstream iss (lines[i]);
std::string dummy; //used to munch the initial `a:` part of the string which you don't need
iss >> dummy;
int numberOfColumns = 0;
char x;
while(iss >> x)
++numberOfColumns;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.