除了使用 getline() 和使用字符串流转换之外,还有什么其他方法可以从 ifstream 读取双倍
What are other ways to read double from an ifstream besides using getline() and converting with stringstream?
我已经编写了以下内容来从输入文件中读取双精度,但这似乎很麻烦,我还可以使用什么其他方法?我应该注意哪些优点/缺点?
我认为这种方法是最准确的,因为不应该有任何二进制<>十进制转换问题,对吗?
#include<string>
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
void Stats::fill()
{
string temp;
stringstream convert;
statFile.open("statsinput.txt");
for(int i = 0; i<maxEntries && !statFile.eof(); i++)
{
getline(statFile, temp);
convert<<temp;
convert>>stats[i];
}
statFile.close();
}
直接在文件中使用输入运算符?
for(int i = 0; i<maxEntries && statFile >> stats[i]; i++)
;
请记住,所有输入流都继承自相同的基类,因此您可以对流(如 stringstream
)或cin
所有其他输入流执行的所有操作
如果你有现代C++(C++11)编译器,你可以使用std::stof,std::stod或std::stelled函数。
然后你可以写:
getline(statFile, temp);
double d = std::stod(temp);
有关C++参考页面的更多信息
相关文章:
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- GlobalAlloc而不是其他分配方法
- 让bool方法返回其他整数
- 还有其他方法可以在数组中写入多维数组吗?
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- Unity3d 中还有其他方法可以访问设备相机吗?
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- Sizeof返回的是指针大小,而不是数组大小.有其他方法可以找到尺寸吗
- 继承构造函数和其他变量的解决方法
- 还有其他方法可以为乘法表编写循环以获取运行时值吗?
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 如何使 extern 方法在共享库中定义,但使用 cmake 在其他目标中声明?
- isdigit() 和 isalnum() 给出错误,因为输入是一个常量字符并且无法转换。其他可能查看输入是否为数字的方法?
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 将指针类方法作为参数传递给其他类方法C
- 如何为其他类成员函数编写模板包装方法
- std::d eclare_if 或其他在编译时丢弃成员声明的假设方法
- 当从其他方法返回 vector 时,C++无法访问矢量元素
- 我不允许更改变量的声明,我可以编辑哪些其他方法
- 如何使用Python脚本或其他一些方法来美化所有文件