C 如何通过特定n字符读取TXT文件的值
How c++ reads the values from txt file by specific n characters
我有一个文本文件,里面有许多值,我想读取此文件。每个值的长度为20个字符,例如带有负量的-5.3815657119783E-04
是20个字符,而在3个字符之前, 3.4684258817593E-02
在lengh中也是20个字符。在我的原始方法中,我想使用std::stringstream
实现此功能,但是stringstream
将值分解为whitespace。
我的大多数数据已连接在一起。因此,我想每20个字符读取此文件以形成一个值,这意味着我想将数据分为C ,然后产生1 D向量。
。这是数据文件:
-5.3815657119783E-04-6.8673010013991E-01-7.5323285000788E-03
3.4684258817593E-02 7.8204534345607E-02-9.0749590089286E-21
-3.2288090365673E-01 9.1882352987172E-02-3.6568412172092E-01
-1.6851899589453E-13-3.6952158259739E-06-1.7702955653531E-07
-1.3297325992783E-06-5.9693615642877E-04-3.8099426849223E-08
3.4698984898706E-08-4.6509379769221E-12-2.2296405498928E-02
-5.2019999391601E-14-4.7969995006506E-08 5.6662120105254E-08
8.9017338669484E-08-2.9332683813429E-06 1.0647933483993E-06
-6.7543843798968E-05-2.1529934384702E-03 2.2028879943185E-05
1.1715465910941E+05-2.5234840649194E+05 1.2213290262328E+05
6.1143067398521E-03 1.0479815336955E-04 7.8911962315577E-08
7.2476042335761E-01 4.1208576787560E-03 0.0000000000000E+00
5.3389720849081E-03 8.4526321374548E-05 4.8860066505864E-08
7.1085208590414E-06 4.5249593432595E-01 4.1468076430511E-04
5.6630655497271E-10 4.0969474876063E-11 9.7240386803972E-05
6.5005706844622E-11 5.1549675717799E-04 8.1291425432847E-18
3.4017603643097E-07 4.4928090110890E-03 1.8886378497020E-10
6.2728934586839E-11 4.7522407515395E-08 3.3417538614997E-07
1.9670991535049E-07 1.9522239039334E-08 2.7359845813293E-18
我在C 中的原始代码:
std::vector<double> value;
ifstream infile;
infile.open("test-file");
int start_line_ = 0;
while (!infile.eof())
{
string line;
getline(infile, line);
if(rows >= start_line_)
{
double number;
std::stringstream stream(line);
while(stream >> number)
{
value.push_back(number);
}
}
infile.close();
}
我的代码将使一些值连接在一起,因为它们不会被空间分开。
如果您要基于输入文件中的20个字符字符串来构建双打矢量,则可以执行此操作:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
int main()
{
std::vector<double> values;
std::ifstream infile;
infile.open("test-file");
std::string line;
// Read each line into a string
while (std::getline(infile, line))
{
// Iterate every 20 chars in the current line
for (size_t i = 0; i < line.size(); i += 20) {
// Get 20 char substring from current position,
// put into a stringstream
std::istringstream os(line.substr(i, 20));
// Put the stringstream into a double and push this to the values vector
double d;
os >> d;
values.push_back(d);
}
}
infile.close();
// Print them to check
for (auto& el : values)
std::cout << el << 'n';
return 0;
}
循环穿过输入中的每一行,将线分解为20个char子字符串。
通过std::istringstream os
将每个子字符串转换为双重,然后将其推到std::vector<double>
。
参考
- c 中的子字符串,带有字符串:: substr((
- Stringstream类
,因为您已经在字符串中拥有该行:
for (int i = 0; i + 20 <= line.size(); i += 20) {
double number;
istringstream(line.substr(i, 20)) >> number;
value.push_back(number);
}
相关文章:
- 一种在C++中读取TXT配置文件的简单方法
- Arduino从Txt读取整数
- Visual Studio在尝试读取resource.txt文件时崩溃
- 如何从txt文件中读取多个不同长度的数组?
- 在一次迭代中从 txt 文件中读取多行
- 我可以读取静态对象中的文件.txt吗?C++
- 在Cmake构建CmakeList.txt中读取/解析XML文件
- 从.txt文件中读取浮点型数字并在公式中使用它们
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 如何使用c ++读取带有非英文字母的*.txt文件?(国际化)
- 如何将变量从 CMakeLists.txt 读取到 cpp 文件
- 尝试从 txt 读取数据
- C++,从 txt 读取到矢量的双倍,读取整个文件时出现问题
- 编写一个C++ pgm 以从文件"input.txt"读取,每当遇到句点时插入换行符并将修改的内容写入"output.txt"
- C++从 txt 读取数字,直到换线(逻辑回归)
- 使用 2D 矢量从 txt 读取文件
- C++从.txt读取浮点值,并将它们放入未知大小的 2D 数组中
- 从文件.txt读取字符串数组并使用它来玩康威的生命游戏
- 从.txt [c++]读取代码
- 从.txt读取int,将其保存到vector,然后保存到输出文件