| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 944 人关注过本帖
标题:读取数据的一个C++程序,运行出错,请问问题出在哪?
只看楼主 加入收藏
hsnr
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2010-4-4
结帖率:90.32%
收藏
已结贴  问题点数:10 回复次数:6 
读取数据的一个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语句从屏幕输出发现是一串奇怪的数值,请问问题出在哪?请高手赐教。现行谢过!
搜索更多相关主题的帖子: 运行 数据 
2010-04-06 16:15
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1483
专家分:9055
注 册:2010-3-16
收藏
得分:2 
input_data_file>>Coord[ipoin][idime];
coord这些指针都没有初始化。
2010-04-06 21:19
elf6530789
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2010-4-5
收藏
得分:2 
很复杂!
2010-04-06 21:33
hsnr
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2010-4-4
收藏
得分:0 
回二楼,的确是个问题,可是我把那些指针变量都去掉,也是同样的问题。
2010-04-07 08:01
hsnr
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2010-4-4
收藏
得分:0 
把程序改成如下
#include "stdafx.h"
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;

    int NELEM;
    int NPOIN;
    int NDIME;
    int NNODE;
    int Nx;
    int Ny;
    double Dtime;
    double Ttime;

// Input all basic data of this c++ program

class DataBankbyInput
{
public:
    DataBankbyInput( );
    void InputBasicData();
    ~DataBankbyInput(){};
};


DataBankbyInput::DataBankbyInput( )
{
}
                    

void DataBankbyInput::InputBasicData()
{

    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;

    return;
}


int _tmain(int argc, _TCHAR* argv[])
{
    DataBankbyInput data;
    data.InputBasicData();


    return 0;
}
还是出现相同情况,问题究竟出在哪呢?
2010-04-07 08:05
yyblackyy
Rank: 6Rank: 6
等 级:侠之大者
帖 子:98
专家分:457
注 册:2010-3-31
收藏
得分:2 
楼主的两个文件错误的地方形同的,所以我只说下面的一个文件:
output_data_file<<NELEM<<'  '<<NPOIN<<'  '<<NDIME<<'  '<<NNODE<<'  '<<Nx<<'  '<<Ny<<'  '<<endl;         

    cout<<NELEM<<'  '<<NPOIN<<'  '<<NDIME<<'  '<<NNODE<<'  '<<Nx<<'  '<<Ny<<'  '<<endl;

output_data_file<<Dtime<<'  '<<Ttime<<endl;

    cout<<Dtime<<'  '<<Ttime<<endl;            打上标记的全部改成"    "这才是空格
2010-04-07 15:23
wuhaijie520
Rank: 1
等 级:新手上路
帖 子:3
专家分:2
注 册:2010-4-7
收藏
得分:2 
太复杂了,还没学到
2010-04-07 17:02
快速回复:读取数据的一个C++程序,运行出错,请问问题出在哪?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014877 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved