读取数据的一个C++程序,运行出错,请问问题出在哪?
鄙人写了一个读数据文件的C++程序,程序如下#include "stdafx.h"
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;
static int NELEM;
static int NPOIN;
static int NDIME;
static int NNODE;
static int Nx;
static int Ny;
static double Dtime;
static double Ttime;
static int** Lnods;
static double** Coord;
static double* InitialValue;
// Input all basic data of this c++ program
class DataBankbyInput
{
public:
DataBankbyInput( );
void InputBasicData(int& NELEM,int& NPOIN,int& NDIME,int& NNODE,
int& Nx,int& Ny,double& Dtime,double& Ttime,
int** Lnods,double** Coord,double* InitialValue);
~DataBankbyInput(){};
};
DataBankbyInput::DataBankbyInput( )
{
}
void DataBankbyInput::InputBasicData(int& NELEM,int& NPOIN,int& NDIME,int& NNODE,
int& Nx,int& Ny,double& Dtime,double& Ttime,
int** Lnods,double** Coord,double* InitialValue)
{
int ielem,inode,ipoin,idime;
ifstream input_data_file;
string filename1;
cout<<"enter the name of the input file"<<endl;
cin>>filename1;
input_data_file.open(filename1.c_str());
if(input_data_file.fail())
{
cout<<"File input_data_file could not be opened"<<endl;
exit(2);
}
//
ofstream output_data_file;
string filename0;
cout<<"enter the name of the output file"<<endl;
cin>>filename0;
output_data_file.open(filename0.c_str());
if(output_data_file.fail())
{
cout<<"File output_data_file could not be opened"<<endl;
exit(2);
}
// READ THE FIRST DATA CARD, AND ECHO IT IMMEDIATELY.
input_data_file>>NELEM>>NPOIN>>NDIME>>NNODE>>Nx>>Ny;
output_data_file<<NELEM<<' '<<NPOIN<<' '<<NDIME<<' '<<NNODE<<' '<<Nx<<' '<<Ny<<' '<<endl;
cout<<NELEM<<' '<<NPOIN<<' '<<NDIME<<' '<<NNODE<<' '<<Nx<<' '<<Ny<<' '<<endl;
// READ TIME STEPPING AND SELECTIVE OUTPUT PARAMETERS
input_data_file>>Dtime>>Ttime;
output_data_file<<Dtime<<' '<<Ttime<<endl;
cout<<Dtime<<' '<<Ttime<<endl;
//
for(ielem=0;ielem<NELEM;ielem++)
{
for(inode=0;inode<NNODE;inode++)
{
input_data_file>>Lnods[ielem][inode];
output_data_file<<Lnods[ielem][inode]<<' '<<endl;
}
}
for(ipoin=0;ipoin<NPOIN;ipoin++)
{
for(idime=0;idime<NDIME;idime++)
{
input_data_file>>Coord[ipoin][idime];
output_data_file<<Coord[ipoin][idime]<<' '<<endl;
}
}
for(ipoin=0;ipoin<NPOIN;ipoin++)
{
input_data_file>>InitialValue[ipoin];
output_data_file<<InitialValue[ipoin]<<' '<<endl;
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
DataBankbyInput data;
data.InputBasicData(NELEM,NPOIN,NDIME,NNODE,
Nx,Ny,Dtime,Ttime,
Lnods,Coord,InitialValue);
return 0;
}
数据文件abc.txt内容如下
2 6 2 4 2 2
0.01 1
1 1 2 5 4
2 2 3 6 5
1 0 0
2 1 0
3 2 0
4 0 1
5 1 1
6 2 1
1 0
2 0
3 0
4 0
5 0
6 0
我自认为程序读取数据应该没错,可是程序运行不下去,我用cout语句从屏幕输出发现是一串奇怪的数值,请问问题出在哪?请高手赐教。现行谢过!