程序在尝试加载csv文件后崩溃

时间:2014-12-07 11:38:49

标签: c++ csv crash

再次,我。我有个问题。我尝试运行此代码,当我编译它时,编译器显示没有错误,但运行它后,程序崩溃。 HELP !!!

该计划应该做什么:

它搜索世界卫生组织(世界卫生组织)的疾病和疾病代码数据。提供了两个csv文件(英语和德语版本),您可以选择使用英语和德语进行搜索。我认为程序在尝试从csv文件加载char * -s时会崩溃。

这让我发疯了。

以下是您可以找到整个项目的链接:

LINK

代码:

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <list>
#include <algorithm>

using namespace std;


enum KEY {code, disease} key;
enum LANGUAGE {english, deutsch} language;

char* buffer;


struct lst
{
    char *_code;
    char *_disease;
};

streamsize _buffer = 10000;

bool isPartOf(char* word, char* sentence)
{
    unsigned int i=0;
    unsigned int j=0;

    for(;i < strlen(sentence); i++)
    {
        if(sentence[i] == word[j])
        {
            j++;
        }
    }

    if(strlen(word) == j)
        return true;
    else
        return false;
}






void printList(list<lst>* LST)
{
    for(list<lst>::iterator i= LST->begin(); i != LST->end(); i++)
    {
        cout << i->_code << '\t' << i->_disease << endl;
    }
}

list<lst>* makeList(char* fileName)
{

    int i,j;

    fstream ICDcsv;
    ICDcsv.open(fileName);
    list<lst>* new_list = new list<lst>;

    if(ICDcsv.is_open())
    {
        while(ICDcsv)
        {
            lst* x = new lst;
            ICDcsv.getline(buffer,_buffer);

            i = 0;
            j = 0;
            while (buffer[i] != ';')
            {
                x->_code[i] = buffer[i];
                i++;
            }
            i++;
            while (buffer[i] != ';')
            {
                x->_disease[j] = buffer[i];
                i++;
                j++;
            }

            (*new_list).push_back(*x);
        }
        ICDcsv.close();
    }
    else{cerr << "Error: file error" << endl;}


    return new_list;
}





list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
    switch(key)
    {
    case code:
        for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
        {
            if(!isPartOf(wrd, ( *i )._code))
            {
                delete [] i->_code;
                delete [] i->_disease;
                delete &(*i);
            }

        }                                                                       //i->

    break;

    case disease:
        for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
        {
            if(!isPartOf(wrd, ( *i )._disease))
            {
                delete [] i->_code;
                delete [] i->_disease;
                delete &(*i);
            }
        }

    break;
    }


    return LST;

}

int main()
{

    int choice;
    int program_end=1;

    char* _file;
    char* Search;

    cout << "World Health Institution (WHO)/Weltgesundheitsorganisation" << endl;
    cout << "International Statistical Classification of Diseases and Related Healt Problems (ICD)/Internationale statistische Klassifikation der Krankheiten und verwandter Gesundheitsprobleme" << endl;
    cout << "1 english" << endl;
    cout << "2 deutsch" << endl;

    cout << "your choice/Ihre Auswahl: ";
    cin >> choice;
    cin.clear();

    if(choice == 1)
        language = english;
    else
        language = deutsch;

    switch (language)
    {
        case english:
            {
                _file = "care_icd10_en.csv";

                break;
            }

        case deutsch:
            {
                _file = "care_icd10_de.csv";

                break;
            }
    }


    cout << "0 end/Ende" << endl;
        cout << "1 search for ICD code (e.g. K52.9)/Suche nach ICD Kode (Beispiel K52.9)" << endl;
        cout << "2 search for desease (e.g. Ebola)/Suche nach Krankheit (Beispiel Ebola)" << endl;
        cout << "your choice/Ihre Auswahl: ";
        cin >> program_end;
        cin.clear();

    switch(program_end)
    {

            case 0: break;

            case 1:
                key = code;
                cout << "to search for ICD code/zu suchender ICD Kode: ";
                break;

            case 2:
                key = disease;
                cout << "to search for deseade/zu suchende Krankheit: ";
                break;
    }

    if(program_end != 0)
    {
        cin >> Search;

        list<lst>* test = makeList(_file);

        list<lst>* test2 = listSearch(test,Search,key);

        printList(test);


    }


    return 0;
}

0 个答案:

没有答案