注册 登录
编程论坛 C++教室

程序出错,错误已经找到,但是不太理解,求详解

神龙赖了 发布于 2013-03-22 21:13, 653 次点击
程序代码:
#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;
            }
        }
}

9 回复
#2
azzbcc2013-03-22 21:17
好久不见啦,最近忙啥子哟
#3
peach54602013-03-22 21:29
好长呀
#4
神龙赖了2013-03-22 22:20
最近在到处研究语言走邪恶路线呢,只是学业繁重啊
这道题能不能帮忙看一看啊,感激不尽啊
#5
不玩虚的2013-03-22 23:24
我也在走邪路,在学神马的flex,actionscript.学业重同道中人啊,顶下
#6
azzbcc2013-03-22 23:29
too long AND C++ 不熟,只好帮顶了
#7
wp2319572013-03-22 23:33
光看 还真不容易看呢
#8
rjsp2013-03-23 09:10
楼主还是换个编译器吧,用符合标准的编译器一编译,这两个错误就出来了
主要是 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;
}

#9
小xiong2013-03-23 12:51
。。。
#10
神龙赖了2013-03-23 13:18
回复 8楼 rjsp
嗯我懂了,很有帮助
以前用c声明是FILE *类型的,c++的不是指针有点不适应
对了,我用的编译器是vc6.0的不过个人认为不好用
在台式上有个vs2010可以太大了,有2.5G,速度奇慢,功能我基本也只需要其中的一点点,不划算啊
rjsp能不能推荐个编辑器?谢谢
1