while循环使用getline()没有突破

时间:2014-11-18 23:24:09

标签: c++

所以我有这部分代码工作得很好。然后我添加了一些行(它下面的代码),现在它在执行时崩溃了。从我做的测试,它似乎陷入了while循环..我是一个新手,我不知道这些变化是如何影响而

编辑:程序编译没有错误。 执行时会出现此消息: 此应用程序已请求Runtime以不寻常的方式终止它。 现在它不会让我使用调试器......(我正在使用Dev C ++)

int main() {
    char fileName[] = "espion.txt";
    std:: string infoEspion;
    Espion *cur = 0, *first = 0, *last = 0, *add = 0;
    std :: ifstream readFile;

    readFile.open(fileName, std::ios::in);

    if(!readFile.is_open())
        exit(EXIT_FAILURE);

    while(std::getline(readFile, infoEspion)) {
        if(first == NULL){ //head
            add = new Espion;
            add -> name = infoEspion.substr(0,30);
            add -> next = NULL;
            first = add;
            cur = add;
            last = add;
        } else if(findName(first, infoEspion.substr(0,30)) == NULL ) {
            // adding only if not on the list already
            add = new Espion;
            add -> name = infoEspion.substr(0,30);
            add -> next = NULL;
            cur -> next = add;
            last = add;
            cur = add;
        }
    }
    printList(first);
    printList(sortAlpha(first));
    readFile.close();

    system("pause");
    return 0;
}

新代码

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>

struct Pays
{
    std:: string name; /* nom du pays */
    Pays *next; /* pointeur sur le prochain pays */
};

struct Espion
{
    std:: string name; /* nom de l’espion(ne) */
    Espion *next; /* pointeur sur le prochain espion */
    Pays *begListPays; /* tête de liste des pays visités */
    Pays *endListPays;
};

Espion *findName(Espion* ptr, std::string nameToCompare)// returns pointer or NULL
{
    while (ptr)
    {
         if(ptr->nom.compare(nameToCompare) == 0)
              return ptr;
         ptr = ptr->next;
    }
    ptr = NULL;
    return ptr;
}

int main() {
    char fileName[] = "espion.txt";
    std:: string infoEspion;
    Espion *cur = 0, *first = 0, *last = 0, *add = 0, *curP = 0;
    Pays *firstP = 0, *lastP = 0, *addP = 0;
    std :: ifstream readFile;

    readFile.open(fileName, std::ios::in);

    if(!readFile.is_open())
        exit(EXIT_FAILURE);

    while(std::getline(readFile, infoEspion)) {
        if(first == NULL){ //head
            add = new Espion;
            add -> name = infoEspion.substr(0,30);
            add -> next = NULL;
            first = add;
            cur = add;
            last = add;
            addP = new Pays;
            addP-> name = infoEspion.substr(30,20);
            addP-> next = NULL;
            add-> begListPays = addP;
            add-> endListPays = addP;
            //changes up to here run OK!
        } else if(findName(first, infoEspion.substr(0,30)) == NULL ) {
            // adding only if not on the list already
            add = new Espion;
            add -> name = infoEspion.substr(0,30);
            add -> next = NULL;
            cur -> next = add;
            last = add;
            cur = add;
            addP = new Pays;
            addP->name = infoEspion.substr(30,20);
            addP->next = NULL;
            add->begListPays = addP;
            add->endListPays = addP;
        } else {
            addP = new Pays;
            addP-> name = infoEspion.substr(30,20);
            addP-> next = NULL;
            findName(first, infoEspion.substr(0,30))->endListPays->next = addP;
            findName(first, infoEspion.substr(0,30))->endListPays = addP;
        }
    }
    printList(first);
    printList(sortAlpha(first));
    readFile.close();

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果文件以换行符结束,那么while循环没有考虑最后一行是空的,因此substr(30,20)会引发异常。

此外,您的循环代码有不必要的代码重复。您可以大大简化逻辑。

试试这个:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <string>

struct Pays
{
    std::string name; /* nom du pays */
    Pays *next; /* pointeur sur le prochain pays */
};

struct Espion
{
    std::string name; /* nom de l’espion(ne) */
    Espion *next; /* pointeur sur le prochain espion */
    Pays *begListPays; /* tête de liste des pays visités */
    Pays *endListPays;
};

Espion* findName(Espion* ptr, std::string nameToCompare)// returns pointer or NULL
{
    while (ptr)
    {
        if(ptr->name.compare(nameToCompare) == 0)
            return ptr;
        ptr = ptr->next;
    }
    return NULL;
}

int main()
{
    char fileName[] = "espion.txt";
    std::string infoEspion, espionName, paysName;
    Espion *first = 0, *last = 0, *add;
    Pays *addP;
    std::ifstream readFile;

    readFile.open(fileName, std::ios::in);
    if (!readFile.is_open())
        exit(EXIT_FAILURE);

    while (std::getline(readFile, infoEspion))
    {
        if (infoEspion.empty())
            continue;

        espionName = infoEspion.substr(0,30);
        paysName = infoEspion.substr(30,20);

        add = findName(first, espionName);
        if (!add)
        {
            add = new Espion;
            add->name = espionName;
            add->next = NULL;
            add->begListPays = NULL;
            add->endListPays = NULL;
            if (!first) first = add;
            if (last) last->next = add;
            last = add;
        }

        addP = new Pays;
        addP->name = paysName;
        addP->next = NULL;
        if (!add->begListPays) add->begListPays = addP;
        if (add->endListPays) add->endListPays->next = addP;
        add->endListPays = addP;
    }

    printList(first);
    printList(sortAlpha(first));

    readFile.close();

    system("pause");
    return 0;
}

话虽如此,您应该利用为您管理更多逻辑的C ++功能:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <string>
#include <list>
#include <algorithm>

struct Pays
{
    std::string name; /* nom du pays */
};

struct Espion
{
    std::string name; /* nom de l’espion(ne) */
    std::list<Pays> ListPays; /* tête de liste des pays visités */

    bool operator<(const Espion& rhs) const
    {
        return (name < rhs.name);
    }
};

struct isEspionName
{
    std::string _name;
    isEspionName(const std::string &nameToCompare) : _name(nameToCompare) {}

    bool operator()(const Espion& e) const
    {
        return (e.name == _name);
    }
};

int main()
{
    char fileName[] = "espion.txt";
    std::string infoEspion, espionName, paysName;
    std::list<Espion> ListEspion;
    Espion *e;
    std::ifstream readFile;

    readFile.open(fileName, std::ios::in);
    if (!readFile.is_open())
        exit(EXIT_FAILURE);

    while (std::getline(readFile, infoEspion))
    {
        if (infoEspion.empty())
            continue;

        espionName = infoEspion.substr(0,30);
        paysName = infoEspion.substr(30,20);

        std::list<Espion>::iterator iter = std::find_if(ListEspion.begin(), ListEspion.end(), isEspionName(espionName));
        if (iter != ListEspion.end())
        {
            e = &(*iter);
        }
        else
        {
            Espion addE;
            addE.name = espionName;
            ListEspion.push_back(addE);
            e = &(ListEspion.back());
        }

        Pays addP;
        addP.name = paysName;
        e->ListPays.push_back(addP);
    }

    readFile.close();

    printList(ListEspion);
    ListEspion.sort();
    printList(ListEspion);

    system("pause");
    return 0;
}

std::list是作为双重链接列表实现的。如果您确实需要单链接列表,C ++ 11会在std::forward_list中添加#include <forward_list>类。