如何仅从C 中的文本文件中读取数字
How to read in only numbers from a text file in C++
我试图从一个看起来像这样的文件中阅读每个月的平均降雨量:
1月3日2月1日2月1.2日2.2 2.2
8月2日9月2日2.4
我需要接受前3个数字,并获得平均值,并在第3个月(3月(中输出。我目前有此代码:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
string name;
double num = 0, many = 0, total = 0, value = 0;
inputFile.open("Rainfall.txt");
for (int count = 1; count <= 6; count++)
{
inputFile >> name;
if (count == 1 || count == 3 || count == 5)
{
continue;
}
name += total;
cout << name << endl;
}
for (int inches = 1; inches <= 6; inches++)
{
inputFile >> name;
if (inches == 1 || inches == 3 || inches == 5)
{
continue;
}
cout << name << endl;
}
inputFile.close();
return 0;
}
,输出看起来像:
3.2
1.2
2.2
2.3
2.4
2.4
现在我无法添加前3个数字,因为它们是字符串,我需要它们是双打。
如果格式 name number
是一致的,则可以琐碎地阅读:
std::string name;
double number = 0, total = 0;
while(inputFile >> name >> number)
{
cout << name << ' ' << number << 'n';
total += number;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量