0

我必须读入由“;”分隔的值 从 csv 文件...类型是 int;char[];char[];char[],float 例如:我必须将它们组装成一个新的结构类型,然后将它们插入到该类型的结构数组中......样本文件:

2345678;Meier;Hans;12.10.1985;2.4;      
1234567;Müller;Fritz;17.05.1990;1.9;
8392019;Thomas;Kretschmer;28.3.1920;2.5;
3471144;Mensch;Arbeit;29.2.2013;4.5;
2039482;Test;Test;30.20.2031;2.1;
7584932;Bau;Maschine;02.02.2010;2.3;
2345678;Meier;Hans;12.10.1985;2.4;      
1234567;Müller;Fritz;17.05.1990;1.9;
8392019;Thomas;Kretschmer;28.3.1920;2.5;
3471144;Mensch;Arbeit;29.2.2013;4.5;
2039482;Test;Test;30.20.2031;2.2;
7584932;Bau;Maschine;02.02.2010;2.3;
2345678;Meier;Hans;12.10.1985;2.4;      
1234567;Müller;Fritz;17.05.1990;1.9;
8392019;Thomas;Kretschmer;28.3.1920;2.5;
3471144;Mensch;Arbeit;29.2.2013;4.5;
2039482;Test;Test;30.20.2031;2.1;
7584932;Bau;Maschine;02.02.2010;2.3;
2345678;Meier;Hans;12.10.1985;2.4;      
1234567;Müller;Fritz;17.05.1990;1.9;
8392019;Thomas;Kretschmer;28.3.1920;2.5;
3471144;Mensch;Arbeit;29.2.2013;4.5;
2039482;Test;Test;30.20.2031;2.0;
7584932;doof;doof;02.02.2010;2.3;
2039482;Test;Test;30.20.2031;2.0;
7584932;doof;doof;02.02.2010;2.3;
2039482;Test;Test;30.20.2031;2.0;
7584932;doof;doof;02.02.2010;2.3;  

我写了一些代码,一切正常,输出显示正确,但是文件的最后一行没有正确解析,它向我展示了一些垃圾,这是我的代码:

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char **argv)
{
    struct Stud{
        long matrnr;
        char vorname[30];
        char name[30];
        char datum[30];
        float note;
    };

    const int MAX = 30;
    Stud stud;  
    Stud mystud[30]; // <<-- Array of "Stud" type
    //memset((void*)mystud,0,sizeof(mystud) * sizeof(Stud));
    int wordCounter(0);
    int i(0); //thats the charCounter or index
    int studentCounter(0);
    char wort[MAX];
    //int matrnr;
    //char vorname[MAX];
    //char name[MAX];
    //char datum[MAX];
    //float note;


  FILE * pFile;
  int cnr(0); 


  pFile=fopen("studentendaten.txt","r");  
  if (pFile==nullptr) 
  {
      perror ("Fehler beim öffnen der Datei");
  }

  else
  {       
    while (cnr != EOF) 
    {       
        (cnr=fgetc(pFile)) ;


        if ((char)cnr == '\n') {
            mystud[studentCounter] = stud;
            studentCounter++;                       
            continue;           
        }

        if ((char)cnr == ';') { 

            wort[i] = '\0'; 

            switch (wordCounter % 5) {

                case 0:             
                stud.matrnr = atol(wort);
                break;

                case 1:
                strcpy(stud.name, wort);
                break;

                case 2:
                strcpy(stud.vorname, wort);
                break;

                case 3:
                strcpy(stud.datum,wort);
                break;

                case 4:
                stud.note = atof(wort); 
                break;
            }       

            wordCounter++;          
            i = 0;
            continue;
        }

        if (wordCounter %  5 == 0 && (char)cnr != ';') {        
        wort[i] = (char)cnr;
        i++;
        //stud.matrnr = atol(wort);
        }           

        if (wordCounter % 5 == 1) {
            wort[i] =  (char)cnr;
            i++;
        //strcpy(stud.name, wort);
        }

        if (wordCounter % 5 == 2) {
            wort[i] = (char)cnr;
            i++;
            //strcpy(stud.vorname, wort);
        }

        if (wordCounter % 5 == 3) {
            wort[i] = (char)cnr;
            i++;
            //strcpy(stud.datum,wort);
        }

        if (wordCounter % 5 == 4) {
            wort[i] = (char)cnr;
            i++;
            //stud.note = atof(wort);                       
        }

    }           

    fclose (pFile);
}
for (int i(0) ; i <= studentCounter; i++) {
cout <<mystud[i].matrnr << "    " << mystud[i].name << "    " << mystud[i].vorname <<"    " 
<< mystud[i].datum <<"    " << mystud[i].note << endl;
  //printf("%5ld        %5s      %5s     %5s     %5f     \n",mystud[i].matrnr,mystud[i].name,mystud[i].vorname,mystud[i].datum,mystud[i].note);

}

    return 0;
}

只有当我把

mystud[studentCounter] = stud;
    fclose (pFile);
}

它在最后一行中读取..它应该已经在上面的代码中执行了,但是我找不到错误...

此外,我必须将我的程序分成不同的部分......不同的头类和一个主类,它汇集在一起​​......有什么建议我应该如何分离我的程序?

4

1 回答 1

3

在我看来,你应该首先决定你是用 C 还是 C++ 来做这件事。您已将它标记为 C++,但尽管它有一些看起来有点 C++ 的东西,但代码让我觉得 C 比 C++ 整体要多得多。

如果我在 C++ 中执行此操作,我将首先编写一个小的 ctype facet,将分号分类为空格:

struct field_reader: std::ctype<char> {

    field_reader(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask> 
            rc(table_size, std::ctype_base::mask());

        rc[';'] = std::ctype_base::space;
        return &rc[0];
    }
};

有了这些,读取数据本身就变得微不足道了:

struct Stud{
    long matrnr;
    std::string vorname;
    std::string name;
    std::string datum;
    float note;

    friend std::istream &operator>>(std::istream &is, Stud &s) { 
        return is >> s.matrnr >> s.vorname >> s.name >> s.datum >> s.note;
    }

    // we'll also add an operator<< to support printing these out:
    friend std::ostream &operator<<(std::ostream &os, Stud const &s) { 
        return os << s.matrnr  << "\t"
                  << s.vorname << "\t"
                  << s.name    << "\t"
                  >> s.datum   << "\t"
                  << s.note;
    }
};

我们真的想用 a 来保存它们,而不是使用数组,std::vector我们可以使用一对std::istream_iterators 将文件中的数据读取到向量中:

// Open the file:
std::ifstream in("studentendaten.txt");

// Use the ctype facet we defined above to classify `;` as white-space:
in.imbue(std::locale(std::locale(), new field_reader);

// read all the data into the vector:
std::vector<Stud> studs{std::istream_iterator<Stud>(in),
                        std::istream_iterator<Stud>()};

// show what we read:
for (auto s : studs) 
    std::cout << s << "\n";
于 2013-11-05T14:37:03.557 回答