无法打印CSV文件
Unable to print CSV file
下面的代码可以正常编译,因此没有语法错误。我有麻烦打印出CSV文件,我读到这一点的代码。我不确定是怎么回事,因为这是一个标准的程序,但会感谢一些输入,如何解决这个问题。我希望能够在控制台上打印出s
的内容。您可以看到为什么s
没有在控制台上打印吗?我错过什么了吗?
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include<iterator>
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{
double VehicleID;
double FinancialYear;
double VehicleType;
double Manufacturer;
double ConditionScore;
friend std::istream &operator>>(std::istream &is, Stud &s) {
return is >> s.VehicleID >> s.FinancialYear >> s.VehicleType >> s.Manufacturer >> s.ConditionScore;
}
// we'll also add an operator<< to support printing these out:
friend std::ostream &operator<<(std::ostream &os, Stud const &s) {
return os << s.VehicleID << "t"
<< s.FinancialYear << "t"
<< s.VehicleType << "t"
<< s.Manufacturer << "t"
<< s.ConditionScore;
}
};
int main(){
// Open the file:
std::ifstream in("VehicleData_cs2v_1.csv");
// 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";
}
friend std::istream &operator>>(std::istream &is, Stud &s) {
return is >> s.VehicleID >> s.FinancialYear >> s.VehicleType
>> s.Manufacturer >> s.ConditionScore;
}
忽略逗号完全存在的事实。我能想到的最原始的解决方案是
friend std::istream &operator>>(std::istream &is, Stud &s) {
char comma;
return is >> s.VehicleID >> comma
>> s.FinancialYear >> comma
>> s.VehicleType >> comma
>> s.Manufacturer >> comma
>> s.ConditionScore;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 如何选择在 csv 文件中输出的位置
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 无法读取C++中的 CSV 文件
- 流:CSV 文件中的换行符
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何正确格式化csv文件?
- 正在分析CSV文件-C++
- 如何获取组件和搜索 CSV 文件
- C++ 中的 CSV 文件管理(特别是设置标头和每个标头将分别具有的值)
- 无法逐行读取.csv文件
- 非常快速地阅读 CSV 文件
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- CSV文件之间的差异,使用getline()产生不同的结果
- 将 CSV 文件中的双倍值C++到矢量中
- C++ 将 CSV 文件的列写入向量
- 使用getline读取.csv文件时出现问题
- CSV文件未从缓冲区获取数据
- 错误的值是使用c++从CSV文件数据写入数组