在c++中读取逗号分隔的TXT文件,并将每列放入数组中



c++问题嗨,我有一个用逗号分隔的txt文件。这是学校的数据库。TXT文件看起来像这样:

AI323,12,soma gomez,Wed,department of art AM324,6,tony wang,Tue;Thu, CC+ dance school

我希望我的代码读取txt文件,并把每个列在一个数组/向量。结果应该是这样的:

class_code={} ; num_students={}; teacher_name={}; date={};location={}

提前感谢你的帮助。我对cpp很陌生。

我试着用getline命令把它们放在一个数组中。然而,我正在努力得到的结果。

从CSV-Database中读取记录的一种非常常用的方法是使用std::getline从文件中逐行读取。请在此阅读有关此功能的内容。

然后将这行再次放入流中,即std::istringstream

可以再次使用格式化或未格式化的流提取函数,如>>std::getline,从字符串(stringstream)中提取部分。

这里特别有用的是带分隔符的std::getline函数。然后,您可以提取字符串的各个部分,直到找到某个字符,在您的示例中是逗号(',')。

那么你需要使用现代c++元素来实现一个解决方案。数据和对这些数据进行操作的方法存放在classstruct中。

为了将现实设计到你的程序中,你需要一个数据类型(struct) "Record"和数据类型(struct) "Database"

然后事情会发展。

请看下面的一个例子。您可以根据以下建议构建您的解决方案:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
// One record, so one row in the database
struct Record{
std::string ID{};
int no{};
std::string name{};
std::string day{};
std::string department{};
int number{};
std::string teacher{};
std::string days{};
std::string location{};
// For reading one record in SCV format from a stream with the C++ extraction operator >>
friend std::istream& operator >> (std::istream& is, Record& r) {
// We will read a complete line and check, if this was OK
std::string line{}; char c;
if (std::getline(is, line)) {
// Now we will put this line into a std::istringstream, so that we can extract 
// parts of it using stream functions. Basically again std::getline
std::istringstream iss{ line };
// and now we tryt to extract all parts of the record from the std::istringstream
std::getline(iss, r.ID,',');
iss >> r.no >> c;
std::getline(iss>> std::ws, r.name, ',');
std::getline(iss, r.day, ',');
std::getline(iss, r.department, ',');
iss >> r.number >> c;
std::getline(iss >> std::ws, r.teacher, ',');
std::getline(iss, r.days, ',');
std::getline(iss, r.location, ',');
}
return is;
}
// For writing one record to a stream with the C++ inserter operator >> in CSV format
friend std::ostream& operator << (std::ostream& os, const Record& r) {
return os << r.ID << ',' << r.no << ',' << r.name << ',' << r.day << ',' << r.department << ',' <<
r.number << ',' << r.teacher << ',' << r.days << ',' << r.location;
}
};
struct Database {

// Here we will store all records
std::vector<Record> records{};
// This will read a complete CSV database from a stream
friend std::istream& operator >> (std::istream& is, Database& d) {
// Clear/delete potential existing old data
d.records.clear();
// Now read complete database from stream
for (Record r; is >> r; d.records.push_back(r))
;
return is;
}
// And this will write all records to a stream;
friend std::ostream& operator << (std::ostream& os, const Database& d) {
for (const Record& r : d.records)
os << r << 'n';
return os;
}
bool load(const std::string& path) {
// Open file and check, if it could be opened
std::ifstream ifs(path);
if (ifs) {
// Read all data
ifs >> *this;
}
else
// Show error message
std::cerr << "nn*** Error: Could not open file '" << path << "'nn";
// return status of operation
return not ifs.bad();
}
bool save(const std::string& path) {
// Open file and check, if it could be opened
std::ofstream ofs(path);
if (ofs) {
// Read all data
ofs << *this;
}
else
// Show error message
std::cerr << "nn*** Error: Could not open file '" << path << "'nn";
// return status of operation
return not ofs.bad();
}
// Please add all functions here to work with your data in the database
// Example: pretty print
void display() {
for (const Record& r : records)
std::cout << r.ID << 'n' << r.no << 'n' << r.name << 'n' << r.day << 'n' << r.department << 'n' <<
r.number << 'n' << r.teacher << 'n' << r.days << 'n' << r.location << 'n';
}
};

const std::string Filename{"database.txt"};
int main() {
Database database{};
database.load(Filename);
database.display();
database.save(Filename);
}

最新更新