Apr 14, 2011 at 2:46pm UTC
hi i need to make a program to count the columns and rows, and subsequently use this matrix load
I did this program, but I have a file that I have no idea how many rows and columns you have, and many want to help me with a program to count the columns and rows:
#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip.h>
double** read_file(std::string filename,int rows,int cols)
{
std::ifstream file;
file.open("D:\\programita\\data.txt", std::ifstream::in);
std::ofstream file2;
file2.open("D:\\programita\\fichero2.txt", std::ofstream::out);
if(!file.is_open()){return 0;}
double** floats = new double*[cols+1];
for(int i = 0; i <cols;++i){ floats[i] = new double[rows+1]; }
double lat, lon, hae, H, deltah, n, rad;
//read each row
for(int i =0;i<rows;++i)
{
for(int j =0;j<cols;++j)//push into the col
{ file >>floats[j][i]; }
// calculos
{ file2.setf(ios::fixed,ios::floatfield);
file2<< lat<<"\t"<< lon<<"\t"<< hae<<"\t"<< H<<"\t"<< deltah<<"\t"<< n<<"\t"; }
file2 <<"\n";
}
file.close();
file2.close();
return floats;
}
int main()
{
int rows = 13;
int cols = 8;
double** floats;
if( !(floats = read_file("test",rows,cols) ) ){return 0;}
system("pause");
return 0;
}
Last edited on Apr 14, 2011 at 3:05pm UTC