你好我在C ++上划分两个双打有问题,在基本的C ++上运行正常,例如
double DfirstNumber = 4.3;
double DsecondNumber = 2.0;
double DthirdNumber = DfirstNumber/DsecondNumber;
std::cout << DthirdNumber;
但不在下面的代码中:
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <regex>
std::string getLastLine(std::ifstream& in)
{
std::string line;
while (in >> std::ws && std::getline(in, line)) // skip empty lines
;
return line;
}
int main()
{
double DfirstNumber;
double DsecondNumber;
while(true){
std::ifstream file("C:\\Users\\Admin\\Documents\\myfile.txt");
if (file)
{
std::string line = getLastLine(file);
Sleep(1000);
try {
std::regex re("\\d*\\.\\d*");
std::sregex_iterator next(line.begin(), line.end(), re);
int i = 0;
std::string firstNumber;
std::string secondNumber;
while (i < 3) {
std::smatch match = *next;
if (i == 1) {
firstNumber = match.str();
DfirstNumber = std::stof(firstNumber);
}
if (i == 2) {
secondNumber = match.str();
DsecondNumber = std::stof(secondNumber);
}
next++;
i++;
}
}
catch (std::regex_error&) {
std::cout << "regex error";
}
double DthirdNumber = DsecondNumber) / DfirstNumber;
std::cout << DthirdNumber << std::endl;
}
else{
std::cout << "Can't Open the file.\n";
}
}
}
我每秒都会在myfile.txt
上获得一个新行,所以我必须检查文件的最后一行,然后使用正则表达式获取所需的数据并将它们存储到C ++变量中。
这就是myfile.txt的外观
Hello Name Name0.00042Surname NameSurname Name0.00042$100.03
Hello Name Name0.00143Surname NameSurname Name0.00143$100.53
Hello Name Name0.00342Surname NameSurname Name0.00342$100.32
..............................................^1stNr^.^2ndNr^
... and another program just continues to extract lines like these every second!
有人可以向我解释为什么会发生这种情况,因为如果我试图分裂的话
DfirstNumber = 407.33
DsecondNumber = 0.015982
我没有收到25486.79764735327
,但我收到25489.8