解析文本文件C++;从特定行到特定行 所以,我对C++是新的。我的任务是解析如下所示的文本文件: RE002%% RE002%% RE002%% RE002%% RE002%% RE004%on% $GPGGA,124749.80,5543.3227107,N,03739.1366738,E,1,08,1.11,147.9635,M,14.4298,M,,*5C $GPGSV,3,1,10,27,13,078,43,05,31,307,48,16,24,042,43,02,10,267,43*7D $GPGSV,3,2,10,26,03,031,36,07,75,215,51,09,57,121,53,30,40,234,50*76 $GPGSV,3,3,10,23,29,117,46,04,36,114,46*70 $GPGGA,124749.90,5543.3227105,N,03739.1366737,E,1,08,1.11,147.9664,M,14.4298,M,,*54 RE005%off%

解析文本文件C++;从特定行到特定行 所以,我对C++是新的。我的任务是解析如下所示的文本文件: RE002%% RE002%% RE002%% RE002%% RE002%% RE004%on% $GPGGA,124749.80,5543.3227107,N,03739.1366738,E,1,08,1.11,147.9635,M,14.4298,M,,*5C $GPGSV,3,1,10,27,13,078,43,05,31,307,48,16,24,042,43,02,10,267,43*7D $GPGSV,3,2,10,26,03,031,36,07,75,215,51,09,57,121,53,30,40,234,50*76 $GPGSV,3,3,10,23,29,117,46,04,36,114,46*70 $GPGGA,124749.90,5543.3227105,N,03739.1366737,E,1,08,1.11,147.9664,M,14.4298,M,,*54 RE005%off%,c++,string,parsing,C++,String,Parsing,它连续运行几千行。我需要找到它将RE004%写入%的位置,并在这个循环中开始处理行,直到它找到RE005%关闭%,并反复执行,直到文件结束。我试着用line.find来解决这个问题,但我很确定这是解决这个问题的错误方法 #include <iostream> #include <fstream> #include <string> #include <stdlib.h> using namespace std; int main()

它连续运行几千行。我需要找到它将RE004%写入%的位置,并在这个循环中开始处理行,直到它找到RE005%关闭%,并反复执行,直到文件结束。我试着用line.find来解决这个问题,但我很确定这是解决这个问题的错误方法

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>



using namespace std;




int main() {
string line, dollar, star, Checksum; 
float *t0 = NULL; 
int tount = 0;  
int k; 

    ifstream logs_("C:/Users/Olya/Desktop/LogGLO.txt"); 
    ofstream tout("outLOGTime.txt"); 
    ofstream pout("outLOGPot.txt"); 
    if (logs_.is_open()) 
    {
        while(getline(logs_,line))
        {   
            line.find("RE004%on%")
            k = 0;

            if 
                dollar = line.find_first_of('$');
                star = line.find_first_of('*');
                Checksum = line.substr(line, dollar, star - dollar);
                for (size_t i = 0; i < Checksum.size(); i++)
                    {

                    }


                if (line.substr(0,6) == "$GPGSV") 
                {
                    for (size_t i = 0, N = 7; i < line.size(); i++)
                    {
                        if (line[i] == ',') k++;
                        if(k == N)
                        {
                        pout << line.substr(i+1,2) << endl;
                        if ((N += 4) > 19) break;
                        }
                    }

                }

        logs_.close(); 

        }
    }
    else 
        cout<<"File is not open"<<'\n';
    tout.close(); 
    pout.close(); 

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串行、美元、星形、校验和;
float*t0=NULL;
int-tount=0;
int k;
ifstream日志(“C:/Users/Olya/Desktop/LogGLO.txt”);
流兜售(“outLOGTime.txt”);
流撅嘴(“outLOGPot.txt”);
如果(日志是打开的())
{
while(getline(logs,line))
{   
行。查找(“RE004%在%”上)
k=0;
如果
美元=行。首先查找(“$”)中的“$”;
星=行。首先查找('*');
校验和=行.substr(行,美元,星-美元);
对于(size_t i=0;ipout不幸的是,您的描述非常不清楚。另外,通过阅读您的代码,我真的无法理解您的意图。并且您编辑了您的文本并更改了描述。对我来说不是那么容易

但是,我做了一个有根据的猜测

我读取给定分隔符之间的所有数据,验证校验和并将行拆分为标记。最后,我将所有带标记的行存储在一个向量中。然后过滤特定值并输出一列

请学习并试着去理解。事情并没有那么复杂

多谢各位

#include <iostream>
#include <regex>
#include <vector>
#include <iterator>
#include <string>
#include <utility>
#include <algorithm>
#include <functional>
#include <numeric>
#include <fstream>

const std::regex re{ R"(\$(.*)\*[abcdefABCDEF\d]{2})" };
const std::regex delimiter{ "," };
using Tokens = std::vector<std::string>;

std::tuple<bool, Tokens> checkString(const std::string& str) {

    // Return value of the function. Assume that string is not ok
    std::tuple<bool, std::vector<std::string>> result(false, {});

    // We want to find a string in the given format
    std::smatch sm{};
    if (std::regex_match(str, sm, re)) {

        // OK, found. Validate checksum
        if (std::string s = sm[1];std::stoul(str.substr(str.size() - 2), nullptr, 16) == std::accumulate(s.begin(), s.end(), 0U, std::bit_xor<unsigned char>())) {

            // Tokenize string
            Tokens tokens(std::sregex_token_iterator(str.begin(), str.end(), delimiter, -1), {});
            // Build return value
            result = std::make_tuple(true, std::move(tokens));
        }
    }
    return result;
}

int main() {

    std::vector<Tokens> csvData{};

    // Open file and check if it is open
    if (std::ifstream logs("r:\\LogGLO.txt"); logs) {

        // Shall we process text lines or not
        bool processingActive{ false };

        // Read all lines of files
        for (std::string line{}; std::getline(logs, line);) {

            // Check, if we should start or stio processing of the lines
            if (line.substr(0, 9) == std::string("RE004%on%")) processingActive = true;
            if (line.substr(0, 10) == std::string("RE005%off%")) processingActive = false;

            // Check and read csv data
            if (processingActive) {
                const auto [ok, data] = checkString(line);
                if (ok) csvData.push_back(std::move(data));
            }

        }
    }

    // So, now we have read all csv data
    // Show eight column of GPGSV data
    for (const Tokens& t : csvData)
        if (t[0] == "$GPGSV") 
            std::cout << t[7] << "\n";

    return 0;
}

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
const std::regex re{R“(\$(.*)\*[abcdefABCDEF\d]{2})”;
常量std::regex分隔符{“,”};
使用Tokens=std::vector;
std::tuple checkString(const std::string和str){
//函数的返回值。假设字符串不正常
std::tuple result(false,{});
//我们希望找到给定格式的字符串
std::smatch sm{};
if(std::regex_匹配(str、sm、re)){
//好的,找到了。验证校验和
if(std::string s=sm[1];std::stoul(str.substr(str.size()-2),nullptr,16)=std::accumulate(s.begin(),s.end(),0U,std::bit_xor()){
//标记化字符串
Tokens Tokens(std::sregx_token_迭代器(str.begin(),str.end(),delimiter,-1),{});
//构建返回值
结果=std::make_tuple(true,std::move(tokens));
}
}
返回结果;
}
int main(){
std::vector csvData{};
//打开文件并检查它是否已打开
if(std::ifstream日志(“r:\\LogGLO.txt”);日志){
//我们是否要处理文本行
布尔处理活动{false};
//读取所有文件行
for(std::string line{};std::getline(logs,line);){
//检查,我们是否应该开始或继续处理这些行
if(line.substr(0,9)==std::string(“RE004%on%”)processingActive=true;
if(line.substr(0,10)==std::string(“RE005%off%”)processingActive=false;
//检查并读取csv数据
if(processingActive){
常量自动[确定,数据]=检查字符串(行);
如果(确定)csvData.push_back(std::move(data));
}
}
}
//现在,我们已经读取了所有csv数据
//显示八列GPGSV数据
for(常量令牌&t:csvData)
如果(t[0]=“$GPGSV”)

非常感谢您编写这个程序,它的许多部分工作我都不清楚,因为我从来没有遇到过您使用的许多函数。有没有可能不使用向量和动态内存就让它以同样的方式运行?