gpt4 book ai didi

c++ - 如何根据文本文件在 C 和 C++ 中的启动方式仅加载某些行?

转载 作者:行者123 更新时间:2023-11-28 07:02:13 25 4
gpt4 key购买 nike

我在一个文件中有一些数据,如下图:

[$GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPRMC,151018.000,A,5225.9627,N,00401.1624,W,0.11,104.71,210214,,*14, $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72, $GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPGSV,3,1,12,26,80,302,44,09,55,063,40,05,53,191,39,08,51,059,37*79, $GPGSV,3,2,12,28,43,112,34,15,40,284,42,21,18,305,33,07,18,057,27*7E,

该文件包含数千行,每一行都以包含特定数据的特定标记开头。

我只需要以 $GPGGA 和 $GPGSV 开头的行。

我如何才能只将这些行加载到链表类型结构中并保持它们的顺序,而不必加载我不需要的其他行?

这是我的一些代码。fscanf 的细节尚不清楚。我所知道的就是将文件逐行加载到链表中。

 if ((head)==NULL){
(head) = malloc(sizeof(GPSList));
current = (head);
}else{
while(current->next!=NULL){
current = current->next;
}
}



while( fscanf(fa,"", ) != EOF) {//format will be done later
// Create new ship here.
strcpy(current->dataGPS.Latitude, Latitude);
strcpy(current->dataGPS.Longitude, Longitude);

current->dataGPS.Time = Time;
current->dataGPS.NumSat = NumSat;
current->dataGPS.SNR =SNR;




GPSList *next = malloc(sizeof(GPSList));
current->next = next;
current = next;

最佳答案

C++代码:

std::string line;

while(std::getline(xInFile, line)) {
// Check if a line starts with what you need

if(std::regex_match(line, "/^(?:(?:\$GPGGA)|(?:\$GPGSV)).*$/")) {
// Use the line. For instance, put it to some array.
};
}

如何逐行读取文件:How do I read long lines from a text file in C++?

关于正则表达式的更多信息:Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

关于 std::regex_match() 的更多信息:std::regex_match - cppreference.com

关于 std::getline() 的更多信息:std::getline - cppreference.com

关于c++ - 如何根据文本文件在 C 和 C++ 中的启动方式仅加载某些行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275576/

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