0

我有一个读取函数,它读取外部文件并使每个名称成为一个新节点,我想在我创建的列表上构建,但不知何故,列表中存在一个空白

example:
I enter in dave, jack, dog, cat. 

result:
name: dave
name: jack
name: dog
name: cat

它打印出来,这就是现在的列表。但是如果我关闭程序并再次运行它,读取函数会读回这些名称并再次创建链接列表。但是如果我选择添加另一个名字“bob”。它把它放在下一个节点

example:
I enter in bob

result:
name: dave
name: jack
name: dog
name: cat
name: [BLANK LINE]
name: bob

6.cpp

#include "6.h"

int main()
{
    petAdoption adopt;
    char response;
    adopt.read();
    do {

    adopt.enroll();
    adopt.display();
    cout << "Another? (y/n): ";
    cin >> response;
    cin.ignore(100,'\n');
    } while (toupper(response) == 'Y');

}

petAdoption::petAdoption()
{
head = NULL;
}

void petAdoption::enroll()
{
    char temp[TEMP_SIZE];
    cout << "Name: ";
    cin.getline(temp, TEMP_SIZE);
    pet.name = new char[strlen(temp)+1];
    strcpy(pet.name, temp);

    newNode = new animal;
    newNode->name = pet.name;
    newNode->next = NULL;

    if (NULL == head)
    {
        head = newNode;
        current = newNode;
    }
    else 
    {               
        current = head;
        while (current->next != NULL){
        current = current->next;
        }
        cout << "adding node to the end of the list" << endl;
        current->next = newNode;
        current = current->next;
    }
        ofstream write;
    write.open("pets.txt", ios::app);
        write << current->name << '\n'; 
    write.close();
}

void petAdoption::display()
{
    current = head;
    while (NULL != current)
    {
        cout << "Pet's name: " << current->name << endl;
        current = current->next;
    }
}

void petAdoption::read()
{
    ifstream read;  
    char temp[TEMP_SIZE];
    read.open("pets.txt");
        if(!read)
        {
            cout << "/#S/ There are no pets" << endl;
        }
        else{
            while(!read.eof())
            {
                read.getline(temp, TEMP_SIZE);
                pet.name = new char[strlen(temp)+1];
                strcpy(pet.name, temp);

                newNode = new animal;
                newNode->name = pet.name;
                newNode->next = NULL;

                if (NULL == head)
                {
                    head = newNode;
                    current = newNode;
                }
                else 
                {               
                    current = head;
                    while (current->next != NULL){
                        current = current->next;
                    }
                    current->next = newNode;
                    current = current->next;
                }
            }
        }
    read.close();
}

6.h

#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

const int TEMP_SIZE = 250;

struct animal
{
    char *name;
    animal *next;   
};

class petAdoption
{
    public:
    petAdoption();
    //~petAdoption();
    void enroll();
    void read();
    void display();
    private:
    animal *head, *current, *newNode;
    animal pet;
};
4

1 回答 1

1
while(!read.eof())
{
    read.getline(temp, TEMP_SIZE);
    ...
}

不是在 C++ 中编写读取文件循环的方法。仅在尝试读取文件末尾后才eof()返回 true ,因此您执行循环体的次数过多。

编写循环的正确方法如下:

for (;;)
{
    read.getline(temp, TEMP_SIZE);
    if (read.failed())
        break;
    ...
}

或更惯用的说法:

while (read.getline(temp, TEMP_SIZE))
{
    ...
}

这意味着完全相同的事情。

另请注意,我使用failed()而不是eof()- 文件结尾不是读取可能失败的唯一原因。

于 2012-12-05T10:00:18.647 回答