thanks so much. i understand all that have been suggested.
how about doing it in c++ syntax?
i am rather new to c++, and thus cant really convert the syntax in c to c++.
hope that someone can help me out.
It would be better for you to make an attempt first, but in order to kill this in your cross-posting, here is one way.
/* file.txt
<matrix>
rows = 2;
cols = 2;
3 2
4 4
</matrix>
*/
#include <iostream>
#include <fstream>
#include <string>
int main(void)
{
const std::string filename("file.txt");
std::ifstream file(filename.c_str());
std::string line;
if ( getline(file, line) && line == "<matrix>" )
{
int rows, cols;
if ( file >> line /* "rows" */ &&
file >> line /* "=" */ &&
file >> rows &&
getline(file,line) /* ; */ )
{
if ( file >> line /* "cols" */ &&
file >> line /* "=" */ &&
file >> cols &&
getline(file,line) /* ; */ )
{
for ( int r = 0; r < rows; ++r )
{
for ( int c = 0; c < cols; ++c )
{
double value;
if ( file >> value )
{
std::cout << value << ' ';
}
}
std::cout << std::endl;
}
}
}
}
return 0;
}
/* my output
3 2
4 4
*/
Reputation Points: 2780
Solved Threads: 312
long time no c
Offline 4,790 posts
since Apr 2004