如何使用文本文件读取特定值 c++ 以存储在数据结构中
How can I read a specific value c++ using a text file to store in data structure
我如何分离我的文本文件值以存储在向量中
我有一个 file.txt,其值为
DOB,NAME,Arrival,Depature
12/04/2021,Dennis,12:30:20,14:30:40
10/03/2001,Sam,14:20:30,-
我想将这些值存储在一个向量中我已经想出如何将它们全部存储在向量中但现在我正在创建另一个向量我想在没有离开时间的情况下存储所有值所以在这种情况下具有此值的 Sam '-'。另一个向量将存储已经有出发时间的丹尼斯
到目前为止我做了什么
name *v = new name;
ifstream myFileStream("FILE.txt");
if(!myFileStream.is_open()){
cout<<"File Failed to open"<<endl;
}
string date,name,arrival,depature;
std::string delimiter = "/";
std::string delimiters = ":";
string line;
string b;
string c;
string d;
string e;
std::getline(myFileStream,line);
while(getline(myFileStream,line)){
stringstream ss(line);
getline(ss,date,',');
//veh.at(i).dt = v->dt;
size_t pos = 0;
std::string token;
while ((pos = date.find(delimiter)) != std::string::npos) {
c=token;
token = date.substr(0, pos);
b=token;
date.erase(0, pos + delimiter.length());
}
v->dt.year = stoi(date);
v->dt.month = stoi(b);
v->dt.day = stoi(c);
veh.at(i).dt.month = v->dt.month;
veh.at(i).dt.year = v->dt.year;
veh.at(i).dt.day = v->dt.day;
getline(ss,v->name,',');
veh.at(i).pltno = v->name;
getline(ss,arrival,',');
size_t pose = 0;
std::string tokens;
while ((pose = arrival.find(delimiters)) != std::string::npos) {
e=tokens;
tokens = arrival.substr(0, pose);
std::cout << tokens << std::endl;
d=tokens;
arrival.erase(0, pose + delimiters.length());
}
v->arrive.hh = stoi(e);
veh.at(i).arrive.hh = v->arrive.hh;
v->arrive.mm = stoi(d);
veh.at(i).arrive.mm = v->arrive.mm;
((stoi(arrival) <= 9) ? 0 : "");
v->arrive.ss = stoi(arrival);
veh.at(i).arrive.ss = v->arrive.ss;
//veh.at(i).arrive = arrival;
getline(ss,depature,',');
//cout<<veh.at(i).arrive<<endl;
i++;
}
myFileStream.close();
伙计们,我真的需要帮助,我现在被困住了,非常感谢有人帮助我编写代码到目前为止,上面的代码将 Name arrival DOB 存储到向量中,但我将创建一个新的离开的人和没有离开的人的向量我如何分离具有“-”和有时间的值并以类似的格式存储它?
谢谢你
#include<vector>
#include<string>
#include<fstream>
using namespace std;
void split(const string& s, vector<string>& tokens, char delim = ' ') {
tokens.clear();
auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t {
for (size_t i = pos; i < s.size(); i++) {
if (s[i] != delim) return i;
}
return string::npos;
};
size_t lastPos = string_find_first_not(0);
size_t pos = s.find(delim, lastPos);
while (lastPos != string::npos) {
tokens.emplace_back(s.substr(lastPos, pos - lastPos));
lastPos = string_find_first_not(pos);
pos = s.find(delim, lastPos);
}
}
struct DOB
{
string year;
string month;
string day;
// DOB(const string &y,const string &m,const string &d):year(y),month(m),day(d){}
DOB(const string &dob)
{
vector<string> strs;
split(dob,strs,'/');
year = strs[2];
month = strs[0];
day = strs[1];
}
};
struct Time
{
bool left;
string hour;
string minute;
string second;
// Time(const string &h,const string &m,const string &s):hour(h),minute(m),second(s){}
Time(const string &time)
{
vector<string> strs;
split(time,strs,':');
hour = strs[0];
minute = strs[1];
second = strs[2];
left = true;
}
Time()
{
left = false;
}
};
struct UserInfo
{
DOB dob;
string name;
Time arrival;
Time depature;
UserInfo(const DOB &d,const string &n,const Time &a,const Time &de):dob(d),name(n),arrival(a),depature(de){}
};
int main()
{
// read and open file
ifstream file;
file.open("./file.txt", ios::in);
if (!file.is_open())
{
// error
return 1;
}
vector<UserInfo> leftUsers;
vector<UserInfo> notleftUsers;
string buf;
getline(file,buf);
// handle file
while (getline(file,buf))
{
if(buf.find("-") == string::npos)
{
// left
vector<string> strs;
split(buf,strs,',');
// DOB
DOB dob(strs[0]);
// Arrival
Time arrival(strs[2]);
Time depature(strs[3]);
leftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
}
else
{
// not left
vector<string> strs;
split(buf,strs,',');
// DOB
DOB dob(strs[0]);
// Arrival
Time arrival(strs[2]);
Time depature;
notleftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
}
}
return 0;
}
我如何分离我的文本文件值以存储在向量中
我有一个 file.txt,其值为
DOB,NAME,Arrival,Depature
12/04/2021,Dennis,12:30:20,14:30:40
10/03/2001,Sam,14:20:30,-
我想将这些值存储在一个向量中我已经想出如何将它们全部存储在向量中但现在我正在创建另一个向量我想在没有离开时间的情况下存储所有值所以在这种情况下具有此值的 Sam '-'。另一个向量将存储已经有出发时间的丹尼斯
到目前为止我做了什么
name *v = new name;
ifstream myFileStream("FILE.txt");
if(!myFileStream.is_open()){
cout<<"File Failed to open"<<endl;
}
string date,name,arrival,depature;
std::string delimiter = "/";
std::string delimiters = ":";
string line;
string b;
string c;
string d;
string e;
std::getline(myFileStream,line);
while(getline(myFileStream,line)){
stringstream ss(line);
getline(ss,date,',');
//veh.at(i).dt = v->dt;
size_t pos = 0;
std::string token;
while ((pos = date.find(delimiter)) != std::string::npos) {
c=token;
token = date.substr(0, pos);
b=token;
date.erase(0, pos + delimiter.length());
}
v->dt.year = stoi(date);
v->dt.month = stoi(b);
v->dt.day = stoi(c);
veh.at(i).dt.month = v->dt.month;
veh.at(i).dt.year = v->dt.year;
veh.at(i).dt.day = v->dt.day;
getline(ss,v->name,',');
veh.at(i).pltno = v->name;
getline(ss,arrival,',');
size_t pose = 0;
std::string tokens;
while ((pose = arrival.find(delimiters)) != std::string::npos) {
e=tokens;
tokens = arrival.substr(0, pose);
std::cout << tokens << std::endl;
d=tokens;
arrival.erase(0, pose + delimiters.length());
}
v->arrive.hh = stoi(e);
veh.at(i).arrive.hh = v->arrive.hh;
v->arrive.mm = stoi(d);
veh.at(i).arrive.mm = v->arrive.mm;
((stoi(arrival) <= 9) ? 0 : "");
v->arrive.ss = stoi(arrival);
veh.at(i).arrive.ss = v->arrive.ss;
//veh.at(i).arrive = arrival;
getline(ss,depature,',');
//cout<<veh.at(i).arrive<<endl;
i++;
}
myFileStream.close();
伙计们,我真的需要帮助,我现在被困住了,非常感谢有人帮助我编写代码到目前为止,上面的代码将 Name arrival DOB 存储到向量中,但我将创建一个新的离开的人和没有离开的人的向量我如何分离具有“-”和有时间的值并以类似的格式存储它?
谢谢你
#include<vector>
#include<string>
#include<fstream>
using namespace std;
void split(const string& s, vector<string>& tokens, char delim = ' ') {
tokens.clear();
auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t {
for (size_t i = pos; i < s.size(); i++) {
if (s[i] != delim) return i;
}
return string::npos;
};
size_t lastPos = string_find_first_not(0);
size_t pos = s.find(delim, lastPos);
while (lastPos != string::npos) {
tokens.emplace_back(s.substr(lastPos, pos - lastPos));
lastPos = string_find_first_not(pos);
pos = s.find(delim, lastPos);
}
}
struct DOB
{
string year;
string month;
string day;
// DOB(const string &y,const string &m,const string &d):year(y),month(m),day(d){}
DOB(const string &dob)
{
vector<string> strs;
split(dob,strs,'/');
year = strs[2];
month = strs[0];
day = strs[1];
}
};
struct Time
{
bool left;
string hour;
string minute;
string second;
// Time(const string &h,const string &m,const string &s):hour(h),minute(m),second(s){}
Time(const string &time)
{
vector<string> strs;
split(time,strs,':');
hour = strs[0];
minute = strs[1];
second = strs[2];
left = true;
}
Time()
{
left = false;
}
};
struct UserInfo
{
DOB dob;
string name;
Time arrival;
Time depature;
UserInfo(const DOB &d,const string &n,const Time &a,const Time &de):dob(d),name(n),arrival(a),depature(de){}
};
int main()
{
// read and open file
ifstream file;
file.open("./file.txt", ios::in);
if (!file.is_open())
{
// error
return 1;
}
vector<UserInfo> leftUsers;
vector<UserInfo> notleftUsers;
string buf;
getline(file,buf);
// handle file
while (getline(file,buf))
{
if(buf.find("-") == string::npos)
{
// left
vector<string> strs;
split(buf,strs,',');
// DOB
DOB dob(strs[0]);
// Arrival
Time arrival(strs[2]);
Time depature(strs[3]);
leftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
}
else
{
// not left
vector<string> strs;
split(buf,strs,',');
// DOB
DOB dob(strs[0]);
// Arrival
Time arrival(strs[2]);
Time depature;
notleftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
}
}
return 0;
}