我必须读取以“;”分隔的值来自csv文件... types是int; char []; char []; char [],float例如: 我必须在一个新的struct类型中组装它们,然后将它们插入到该类型的struct数组中...... 样本文件:
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);
}
它在最后一行读取..它应该在上面的代码中执行它,但我找不到错误...
此外,我必须将我的程序分成不同的部分...不同的标题类和一个主要类,它们汇集在一起......有什么建议我应该如何分离我的程序?
答案 0 :(得分:3)
在我看来,你应该首先决定是否尝试用C或C ++来做这件事。你已经将它标记为C ++,但是虽然它有一些很少看起来含糊不清的C ++东西,但代码让我觉得C语言比C ++总体要多得多。
如果我在C ++中这样做,我首先要编写一个小分区,将分号分类为空格:
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
来将文件中的数据读入向量:
// 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";