| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 653 人关注过本帖
标题:程序出错,错误已经找到,但是不太理解,求详解
只看楼主 加入收藏
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
结帖率:97.22%
收藏
已结贴  问题点数:50 回复次数:9 
程序出错,错误已经找到,但是不太理解,求详解
程序代码:
#include <iostream>
#include <fstream>

#define SIZE 60
char filename[SIZE];

using namespace std;

struct Dntnews
{
    char name[SIZE];
    double money;
};

void rname(ifstream,Dntnews *);

int main(void)
{
    ifstream iDfile;
    Dntnews *Dp = new Dntnews;
   
    /*open the file */
    ofstream oDfile;

    again:
    cout << "请输入需要创建的文件路径及文件名: ";
    cin.getline(filename,SIZE);
   
    oDfile.open(filename);

    if(!oDfile.is_open())
    {
       cout << "文件创建失败,请检查文件路径是否正确!\n\n";
       goto again;
    }

    oDfile << "The donation people's list \n\nName\t\tMoney\n";
    /* open to end */

    cout << "请输入捐款人数目: ";
   
    int people = 0;
    cin >> people;

    for(int i = 1; i <= people; i++)
    {
      cout << "请输入捐款人姓名: ";
      (cin >> Dp->name).get();
      cout << "请输入捐款钱数: ";
      cin >> Dp->money;

      //write to the Dfile
      oDfile << '@';       //name's mark
      oDfile << Dp->name;
      oDfile << "\t\t";
      oDfile << '$';      //money's mark
      oDfile << Dp->money;
      oDfile << "\n";
    }
    oDfile.close();
   
    iDfile.open(filename);
    for(i = 1; i <= people; i++)
    {
        cout << i << endl;
        //rname(iDfile,Dp);
        char ch = 0;
        for(;;)
        {
            if(!(iDfile >> ch))
            {
                cout << "到达文件尾" << ch;
                break;
            }

            cout << ch << " == ch\n";
            if(ch == '@')
            {
                iDfile >> Dp->name;
                continue;
            }
            if(ch == '$')
            {
                iDfile >> Dp->money;
                cout << "运行\n";
                break;
            }
        }
        //end

        cout << "运行2" << people << i << endl;
        if(Dp->money >= 10000)
        {
        cout << Dp->name << "捐赠了" << Dp->money;
        if(Dp->money > 36500)
            cout << "元! 感谢" << Dp->name << endl;
        else
            cout << "元." << endl;
        }
        cout << "运行3";
    }
    cout << "运行4" << endl;
    delete Dp;
    cout << "运行5" << endl;
    iDfile.close();
    cout << "运行6" << endl;
    return 0;
}

void rname(ifstream iDfile,Dntnews *Dp)
{
    char ch = 0;
        for(;;)
        {
            if(!(iDfile >> ch))
            {
                cout << "到达文件尾" << ch;
                break;
            }

            cout << ch << " == ch\n";
            if(ch == '@')
            {
                iDfile >> Dp->name;
                continue;
            }
            if(ch == '$')
            {
                iDfile >> Dp->money;
                cout << "运行\n";
                break;
            }
        }
}
这个程序可以正常运行,但是不用
程序代码:
//rname(iDfile,Dp);
        char ch = 0;
        for(;;)
        {
            if(!(iDfile >> ch))
            {
                cout << "到达文件尾" << ch;
                break;
            }

            cout << ch << " == ch\n";
            if(ch == '@')
            {
                iDfile >> Dp->name;
                continue;
            }
            if(ch == '$')
            {
                iDfile >> Dp->money;
                cout << "运行\n";
                break;
            }
        }
        //end


这一段而调用rname函数程序(其内容一模一样)就会读取文件读取出错
在循环中第二次调用函数的时候文件中的读取位置已经不对了
我想应该是带入函数时变元创建副本的问题,但是第二次为什么直接就出错了呢?就算不在上次读取的位置上也应该在开始的位置啊?不懂求解,分全给,望赐教...
我整理了一下,大致只需看
程序代码:
#include <iostream>
#include <fstream>

#define SIZE 60
char filename[SIZE];

using namespace std;

struct Dntnews
{
    char name[SIZE];
    double money;
};

void rname(ifstream,Dntnews *);

int main(void)
{
    ifstream iDfile;
    Dntnews *Dp = new Dntnews;
   
    /*open the file */
    ofstream oDfile;

    again:
    cout << "请输入需要创建的文件路径及文件名: ";
    cin.getline(filename,SIZE);
   
        cout << "请输入捐款人数目: ";
   
    int people = 0;
    cin >> people;

        iDfile.open(filename);
    for(i = 1; i <= people; i++)
    {
        cout << i << endl;
        //rname(iDfile,Dp);
        char ch = 0;
        for(;;)
        {
            if(!(iDfile >> ch))
            {
                cout << "到达文件尾" << ch;
                break;
            }

            cout << ch << " == ch\n";
            if(ch == '@')             //@和$分别是文件中名字和钱的位置,这里无错,不用考虑
            {
                iDfile >> Dp->name;
                continue;
            }
            if(ch == '$')
            {
                iDfile >> Dp->money;
                break;
            }
        }
        //end

           }
    return 0;
}

void rname(ifstream iDfile,Dntnews *Dp)
{
    char ch = 0;
        for(;;)
        {
            if(!(iDfile >> ch))
            {
                cout << "到达文件尾" << ch;
                break;
            }

            cout << ch << " == ch\n";
            if(ch == '@')
            {
                iDfile >> Dp->name;
                continue;
            }
            if(ch == '$')
            {
                iDfile >> Dp->money;
                cout << "运行\n";
                break;
            }
        }
}

搜索更多相关主题的帖子: money color 
2013-03-22 21:13
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:2 
好久不见啦,最近忙啥子哟


[fly]存在即是合理[/fly]
2013-03-22 21:17
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:2 
好长呀

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-03-22 21:29
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:0 
最近在到处研究语言走邪恶路线呢,只是学业繁重啊
这道题能不能帮忙看一看啊,感激不尽啊

I have not failed completely
2013-03-22 22:20
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:2 
我也在走邪路,在学神马的flex,actionscript.学业重同道中人啊,顶下

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-03-22 23:24
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
too long AND C++ 不熟,只好帮顶了


[fly]存在即是合理[/fly]
2013-03-22 23:29
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:2 
光看 还真不容易看呢

DO IT YOURSELF !
2013-03-22 23:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:40 
楼主还是换个编译器吧,用符合标准的编译器一编译,这两个错误就出来了
主要是 fstream 不具有拷贝复制语义,因此 void rname(ifstream iDfile 是错误的,应当是 void rname(ifstream& iDfile

程序代码:
#include <iostream>
#include <fstream>
#include <string>

struct donation
{
    std::string name;
    double money;
};

bool read_donation( std::ifstream& iDfile, donation& dnt );

using namespace std;

int main(void)
{
    string filename;

    ofstream oDfile;
    while( !oDfile.is_open() )
    {
        cout << "请输入需要创建的文件路径及文件名: ";
        getline( cin, filename );
        oDfile.open( filename.c_str() );
        if( !oDfile )
            cerr << "文件创建失败,请检查文件路径是否正确!\n\n";
    }

    oDfile << "The donation people's list \n\nName\t\tMoney\n";

    cout << "请输入捐款人数目: ";
    size_t num;
    cin >> num;
    for( size_t i=0; i!=num; ++i )
    {
        donation dnt;

        cout << "请输入捐款人姓名: ";
        cin >> dnt.name;
        cout << "请输入捐款钱数: ";
        cin >> dnt.money;

        oDfile << '@' << dnt.name << "\t\t$" << dnt.money << '\n';
    }

    oDfile.close();

    /////////////////////////////////////////////////

    ifstream iDfile( filename.c_str() );
    for( donation dnt; read_donation(iDfile,dnt); )
    {
        cout << dnt.name << "\t\t" << dnt.money << endl;
    }
    iDfile.close();

    return 0;
}

bool read_donation( std::ifstream& iDfile, donation& dnt )
{
    for( char c; iDfile>>c; )
    {
        if( c == '@' )
        {
            iDfile >> dnt.name;
        }
        else if( c == '$' )
        {
            iDfile >> dnt.money;
            return true;
        }
    }
    return false;
}

2013-03-23 09:10
小xiong
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:388
专家分:1722
注 册:2013-2-8
收藏
得分:2 
。。。
2013-03-23 12:51
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:0 
回复 8楼 rjsp
嗯我懂了,很有帮助
以前用c声明是FILE *类型的,c++的不是指针有点不适应
对了,我用的编译器是vc6.0的不过个人认为不好用
在台式上有个vs2010可以太大了,有2.5G,速度奇慢,功能我基本也只需要其中的一点点,不划算啊
rjsp能不能推荐个编辑器?谢谢

I have not failed completely
2013-03-23 13:18
快速回复:程序出错,错误已经找到,但是不太理解,求详解
数据加载中...
 
   



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

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