gpt4 book ai didi

c++ - 分隔字符行形成数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:57 27 4
gpt4 key购买 nike

我只想在文件中存储数据(不包括字符行)。我有这个脚本来读取文件,

#include "TMath.h"
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <math.h>

temp() {

ifstream myfile("scanner.dat");
ifstream input_file("scanner.dat");

int nlines=1;
string line;
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line); nlines++;
}
myfile.close();
}
cout<<"nlines = "<<nlines-1<<endl;
int dim=nlines-1;
string a[dim],b[dim],c[dim],d[dim],e[dim],f[dim],g[dim];

if(input_file.is_open()){
for(int i=0; i<700;i++) {
input_file>>a[i]>>b[i]>>c[i]>>d[i]>>e[i]>>f[i]>>g[i];
cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<" "<<d[i]<<" "<<e[i]<<" "<<f[i]<<" "<<g[i]<<endl;
}
}

}

但问题是字符行不会出现在特定日期行之后。这是输入数据文件的一部分。

# Before scan of next raw/column:
# X Y t I1 I2 I3 I4
0.00 0.00 1.92 330 0 0 0
0.00 0.00 2.92 335 0 0 0
0.00 0.00 3.92 330 0 0 0
0.00 39.99 9.92 345 0 0 0
# Before scan of next raw/column:
# X Y t I1 I2 I3 I4
0.00 0.00 1.92 330 0 0 0
0.00 0.00 2.92 335 0 0 0
0.00 0.00 3.92 330 0 0 0
0.00 39.99 9.92 345 0 0 0
0.00 0.00 3.92 330 0 0 0
0.00 39.99 9.92 345 0 0 0

最佳答案

  • 首先,您需要将整行读取为string
  • 然后通过检查'#'来确定它是否是评论第一个字符。
  • 请记住使用不同的索引计数器,因为某些行会被忽略。 (j 变量在以下示例中使用)

下面有一个例子,

//...
std::string line;
for(int i=0, j=0; i<700;i++)
{
std::getline(input_file, line)
if (line.rfind("#", 0) == 0)
continue; // Ignore the comments.

std::istringstream iss(line)
iss>>a[j]>>b[j]>>c[j]>>d[j]>>e[j]>>f[j]>>g[j];
j++;
//...

关于c++ - 分隔字符行形成数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com