C++:"Low Level".csv数据读入结构,并将其放入数组中

C++: "Low Level" reading .csv data into a structure , and putting it into a sruct array

本文关键字:数组 结构 Level Low csv 数据 C++      更新时间:2023-10-16

我必须从 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] = ''; 
            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);
}

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

此外,我必须将我的程序分成不同的部分......不同的标头类和一个主类,它都聚集在一起......任何建议我应该如何分离我的程序?

在我看来,你应该首先决定你是尝试用 C 还是C++来做到这一点。你已经把它标记为C++,但是尽管它有一些看起来模糊C++的东西,但代码给我的印象是C比C++整体要多得多。

如果我在C++中这样做,我会从编写一个小的 ctype 方面开始,将分号分类为空格:

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;
    }
};

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

// 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";