我有一個CSV數據,它包含此結構:如何在C++中將日期讀入結構?
2345678 Meier Hans 12.10.1985 2.4
int, char[],char[], date,float
我想要從這些數據類型此行一個結構,我必須把結構到一個數組,現在當我來讀日期,我應該在結構中使用什麼變量類型?以及如何將單日,月份和年份的數字讀入我的類型?有沒有一個拆分函數在每個點(「。」)分裂? 我是否應該使用「struct tm ts」並將當天的月份和年份分配給ts.constants? (ts_mday; ts_mmonth等)
我做了什麼至今:
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
struct studentendaten {
int matrnr;
char[] name;
char[] vorname;
struct tm ts;
ts.tm_mday;
ts.tm_mon;
ts.tm_year;
float note;
}
FILE * pFile;
int ch;
int n = 0;
pFile=fopen("studentendaten.txt","r");
if (pFile==nullptr) perror ("Error opening file");
else
{
while (ch != EOF)
{
ch = fgetc (pFile);
if (ch == ';') {n++;}
putchar(ch);
}
fclose (pFile);
}
return 0;
}
我纔去到更多的肉的問題上取得了語法錯誤出你現有的代碼工作。編譯器很擅長告訴你它們在哪裏。 –
若要將日期/時間「字符串」轉換爲二進制結構,您可能需要查看'strptime()'函數。 – alk
你是說你的**逗號** - 分值文件中不包含逗號? –