c++ 文件输入输出流 无法输入 代码如下(真心求急救)

void Node::InputMule()
{
pHead = new NODE[sizeof (Node)];
if(NULL == pHead)
{
cout << "分配动态内存失败" << endl;
exit(0);
}

    PNODE pTail = pHead;
    pTail -> pNext = NULL;

    int number; //  头数
    char name[30];
    int id;
    int age;
    double weight;
    double thingweight;
    char color[30];

    cout << "请输入头数" << endl;
    cin >> number;
    cout << "输入好了" << endl;

    ifstream infile("infile.txt",ios::in);

    for(int i = 0 ; i < number ;i ++){
            PNODE pNew = new NODE[sizeof(NODE)];
            if(NULL == pNew){
                    cout << "分配动态内存失败" << endl;
                    exit(0);
            }


    char s[10];
    if(!infile){
            ofstream outfile("outfile.txt");
            while(infile.peek() == EOF){
                            cout << "love" << endl;
                            infile  >> name ;
                            cout << "me" << endl;
                                    outfile<< name << "*********************************" << "  " << '\n' <<'\t' << endl;
                            infile.read(s,sizeof(s) - 1);
                                    outfile << '\t' <<"骡的名字" << '\t' << name << endl;
                            infile.getline(s,sizeof(s) - 1);
                                    outfile << '\t' <<"骡的年龄" << '\t' << age << endl;
                            infile.getline(s,sizeof(s) - 1);
                                    outfile << '\t' << "骡的体重" << '\t'<< weight << endl;
                            infile.getline(s,sizeof(s) - 1);
                                    outfile << '\t' << "骡的负重" << '\t'<< thingweight << endl;
                                                                            infile.get(s,sizeof(s) - 1);
                                    outfile << '\t' << "骡的颜色" << '\t' << color << endl;
                    }

                    outfile.close();
    }

            pNew -> mu.SetInfo(name,age);
            pNew -> mu.SetId(id);
            pNew -> mu.SetWeight(weight);
            pNew -> mu.SetColor(color);
            pNew -> mu.SumWeight(weight,thingweight);
            pNew -> mu.SetThingweight(thingweight);

            pTail -> pNext = pNew;
            pTail = pNew;
            pTail -> pNext = NULL;

    }

    infile.close();

    cout << "已经成功的向程序中输入了" << number << "头骡信息" << endl;

}

c++

3个回答

  while(infile.peek() == EOF){   //循环条件不对,这么写意思是infile.peek()等于EOF时才执行循环体中的代码。

用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢!

仍然不能输入信息

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!