gpt4 book ai didi

C++ : read csv file with fgetc and separate words on semicolon ";"

转载 作者:行者123 更新时间:2023-11-28 07:19:08 25 4
gpt4 key购买 nike

我必须读入一个包含 5 个字段(int、char[]、char[]、char[]、float)的 csv 文件,看起来像这样:

2345678;Meier;Hans;12.10.1985;2.4;      
1234567;Müller;Fritz;17.05.1990;1.9;

我必须将字段放在一个结构中,然后在完成一行后将结构放入结构类型的数组中......

为了学习效果,我们只能使用LOW-LEVEL编码,只能使用fgetc、strcpy等函数,不能使用字符串,只能使用char[]...现在我让我的算法逐个字符地读取文本文件,但我在正确分离它们、再次将它们组合在一起并将它们正确分配给结构字段时遇到了问题。这是我的代码:

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

我不确定它是否与错误的增量变量有关,或者我没有在我的 wort[] 数组的末尾放置 '\0' 的事实..因此无法识别结束我的阵列?如果是这样,我该怎么做而不知道到底在哪里......? (我不知道单词的长度..)

编辑:我再次更新了我的代码,唯一让我感到奇怪的是 LAST LINE IS NOT BEING CORRECTLY PARSED ,它显示了一些垃圾,我看不到我的代码中的错误...

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;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;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;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;Bau;Maschine;02.02.2010;2.3;

最佳答案

建议:使用case 结构进行解析,自己做一个“copyToSemicolon”函数:然后你可以这样写

sIndexCount = 0;
char temp[50];
while((cnr=fgetc(pFile)) != EOF) {
offset = 0;
for(var = 0; var < 5; var++ {
switch(var) {
case 0:
offset = copyToSemicolon(temp, cnr, offset) + 1;
stud.matrnr = atoi(temp);
break;
case 1:
offset = copyToSemicolon(mystud[sIndexCount].vorname, cnr, offset) + 1;
break;
... etc
}
}
sIndexCount++;
if(sIndexCount == 50) break; // in case the input file is longer than our structure
}

并且您需要一个函数 copyToSemicolon,它将两个 char* 指针作为输入,并从第二个字符串(从 offset 开始)复制字符>) 直到它到达分号或行尾 - 并返回它到达的偏移量(最后一个字符读取)。

int copyToSemicolon(char* dest, char* source, int offset) {
while(source[offset] != ';' && source[offset] != '\n') {
*dest = source[offset++];
dest++;
}
return offset;
}

编辑 strtok 方法:

sIndexCount = 0;
char temp[50];
while((cnr=fgetc(pFile)) != EOF) {
offset = 0;
temp = strtok(cnr, ';');
for(var = 0; var < 5; var++ {
switch(var) {
case 0:
stud.matrnr = atoi(temp);
break;
case 1:
strcpy(mystud[sIndexCount].vorname, strtok(NULL, ';'));
break;
... etc
case 4:
mystud[sIndexCount].note = atof(strtok(NULL, '\n'));
}
}
sIndexCount++;
if(sIndexCount == 50) break; // in case the input file is longer than our structure
}

关于C++ : read csv file with fgetc and separate words on semicolon ";",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com