C++ 使用C++;
我正试图从dat文件中提取一个日期,该日期由int、char和float值组成 Dat文件格式如下所示:C++ 使用C++;,c++,file,io,fstream,istream,C++,File,Io,Fstream,Istream,我正试图从dat文件中提取一个日期,该日期由int、char和float值组成 Dat文件格式如下所示: 201205171200 M29.65 201207041900 F30.3 等等 我正在努力分离这些价值观。 以下是我到目前为止的情况: #include <iostream> #include <fstream> #include <vector> using namespace std; int main() { int in
201205171200 M29.65
201207041900 F30.3
等等
我正在努力分离这些价值观。
以下是我到目前为止的情况:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
int inCount = 0; //This variable will be used to keep track of what record is being read.
vector<int> dates;
vector<float> temps;
// Open and retrieve data from text.
ifstream inFile;
inFile.open("biodata.dat");//Opening Biodata file to begin going through data
if(inFile)
{
char tempType;
while(!inFile.eof())
{
if (inFile.eof()) break;
inFile >> dates[inCount];
cout << dates[inCount];
inFile >> tempType;
inFile >> temps[inCount];
if(tempType == 'F'){
temps[inCount] = (temps[inCount] - static_cast<float>(32)) * (5.0/9.0);
}
inCount++;
}
} else {
cout << "The file did not load";
return 0;
}
}
#包括
#包括
#包括
使用名称空间std;
int main(){
int inCount=0;//此变量将用于跟踪正在读取的记录。
矢量日期;
向量temp;
//打开并从文本中检索数据。
河流充填;
infle.open(“biodata.dat”);//打开biodata文件开始遍历数据
如果(填充)
{
字符类型;
而(!infle.eof())
{
如果(infle.eof())中断;
填充>>日期[计入];
cout>t型;
填充>>临时[计入];
如果(试探类型='F'){
temps[inCount]=(temps[inCount]-静态(32))*(5.0/9.0);
}
inCount++;
}
}否则{
cout声明三个变量,并通过链式提取从文件中读取它们
ifstream inFile("biodata.dat");
std::string date; // since your values are so large
char mf;
double d;
while (inFile >> date >> mf >> d) {
// use the vars here
}
您必须使用足够大的东西来存储数字。如果足够大,您可以使用long
,但它可能不够大。您可以使用static\u assert(std::numeric\u limits::max()声明三个变量,并通过链式提取从文件中读取它们
ifstream inFile("biodata.dat");
std::string date; // since your values are so large
char mf;
double d;
while (inFile >> date >> mf >> d) {
// use the vars here
}
您必须使用足够大的东西来存储数字。如果足够大,您可以使用long
,但它可能不够大。您可以使用static\u assert(std::numeric\u limits::max()声明三个变量,并通过链式提取从文件中读取它们
ifstream inFile("biodata.dat");
std::string date; // since your values are so large
char mf;
double d;
while (inFile >> date >> mf >> d) {
// use the vars here
}
您必须使用足够大的东西来存储数字。如果足够大,您可以使用long
,但它可能不够大。您可以使用static\u assert(std::numeric\u limits::max()声明三个变量,并通过链式提取从文件中读取它们
ifstream inFile("biodata.dat");
std::string date; // since your values are so large
char mf;
double d;
while (inFile >> date >> mf >> d) {
// use the vars here
}
您必须使用足够大的东西来存储数字。如果足够大,您可以使用long
,但它可能不够大。您可以使用static\u assert(std::numeric\u limits::max()来检查这一点。在典型情况下,每行代表一个逻辑记录,您需要从定义一个结构开始(或类)表示这些记录之一。在这种情况下,通常最容易定义运算符>>
从文件中读取一条(且仅一条)记录:
struct record {
unsigned long long timestamp;
float temp;
std::istream &operator>>(std::istream &is, record &r) {
char temp;
is >> r.timestamp >> temp >> r.temp;
if (temp == 'F') {
r.temp -= 32.0f;
r.temp *= 5.0f / 9.0f;
}
return is;
}
};
有了此定义,您就可以在一次操作中读取和删除整个记录:
record r;
infile >> r;
或者,(看起来你可能需要在这里)你可以一次读取它们的整个向量:
std::vector<record> records {
std::istream_iterator<record>(infile),
std::istream_iterator<record>()
};
std::向量记录{
标准::istream_迭代器(infle),
std::istream_迭代器()
};
您可能更喜欢在读取时将时间戳拆分为各个字段。如果是这样,您可以定义时间戳
结构,并对其执行基本相同的操作—定义年、月、日、小时和分钟的字段,然后定义运算符>>
,以单独读取每个字段(读四个字符,将它们转换为整型,再读两个字符,然后转换为月份,等等
有了这个定义,您只需将记录的timestamp成员定义为该类型的实例,并且仍然使用>
流提取器读取该对象的实例,如上所述。在典型情况下,每一行代表一个逻辑记录,您需要从定义一个结构(或类)开始表示其中一条记录。在这种情况下,通常最容易定义运算符>>
从文件中读取一条(且仅一条)记录:
struct record {
unsigned long long timestamp;
float temp;
std::istream &operator>>(std::istream &is, record &r) {
char temp;
is >> r.timestamp >> temp >> r.temp;
if (temp == 'F') {
r.temp -= 32.0f;
r.temp *= 5.0f / 9.0f;
}
return is;
}
};
有了此定义,您就可以在一次操作中读取和删除整个记录:
record r;
infile >> r;
或者,(看起来你可能需要在这里)你可以一次读取它们的整个向量:
std::vector<record> records {
std::istream_iterator<record>(infile),
std::istream_iterator<record>()
};
std::向量记录{
标准::istream_迭代器(infle),
std::istream_迭代器()
};
您可能更喜欢在读取时将时间戳拆分为各个字段。如果是这样,您可以定义时间戳
结构,并对其执行基本相同的操作—定义年、月、日、小时和分钟的字段,然后定义运算符>>
,以单独读取每个字段(读四个字符,将它们转换为整型,再读两个字符,然后转换为月份,等等
有了这个定义,您只需将记录的timestamp成员定义为该类型的实例,并且仍然使用>
流提取器读取该对象的实例,如上所述。在典型情况下,每一行代表一个逻辑记录,您需要从定义一个结构(或类)开始表示其中一条记录。在这种情况下,通常最容易定义运算符>>
从文件中读取一条(且仅一条)记录:
struct record {
unsigned long long timestamp;
float temp;
std::istream &operator>>(std::istream &is, record &r) {
char temp;
is >> r.timestamp >> temp >> r.temp;
if (temp == 'F') {
r.temp -= 32.0f;
r.temp *= 5.0f / 9.0f;
}
return is;
}
};
有了此定义,您就可以在一次操作中读取和删除整个记录:
record r;
infile >> r;
或者,(看起来你可能需要在这里)你可以一次读取它们的整个向量:
std::vector<record> records {
std::istream_iterator<record>(infile),
std::istream_iterator<record>()
};
std::向量记录{
标准::istream_迭代器(infle),
std::istream_迭代器()
};
您可能更喜欢在读取时将时间戳拆分为各个字段。如果是这样,您可以定义时间戳
结构,并对其执行基本相同的操作—定义年、月、日、小时和分钟的字段,然后定义运算符>>
,以单独读取每个字段(读四个字符,将它们转换为整型,再读两个字符,然后转换为月份,等等
有了这个定义,您只需将记录的timestamp成员定义为该类型的实例,并且仍然使用
流提取器读取该对象的实例,如上所述。在典型情况下是eac