gpt4 book ai didi

c - C中的文件解析

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:17 26 4
gpt4 key购买 nike

我正在尝试编写一个基本的 GPS 解析器。我一次接收一个字符的数据并将其放入字符数组中,直到未检测到 CRLF。然而,我的字符数组似乎一次只有一个。还有字母 [100],因为已知最大大小不超过 100。另外,我正在填写垃圾字符“>”,因为我知道它不会成为 GPS 数据包的一部分。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) {
char ch;
char letters[100];
FILE *fp;
int i=0;
for( i=0; i<100; i++)
{
letters[i] ='>';
}
fp = fopen(" wont work in SO :) ","r"); // read mode
if( fp == NULL )
{
printf("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
int pointer = 0;
while( ( ch = fgetc(fp) ) != EOF )
{
if(ch == '\r' || ch == '\n')
{
printf("%c %c", letters[2], letters[3]);// I am trying to check the array correctness here
pointer =0;
for( i=0; i<100; i++)
{
letters[i] ='>'; // reinitialising the array
}

}
letters[pointer] = ch;
pointer++;

}

fclose(fp);
return 0;
}

这是我包含数据的 GPS 文本文件

$GPGLL,4539.99781,N,11102.77595,W,210416.00,A,A*4
$GPRMC,210417.00,A,4539.99764,N,11102.77603,W,0.444,,280909,,,A*65
$GPVTG,,T,,M,0.444,N,0.822,K,A*2F
$GPGGA,210417.00,4539.99764,N,11102.77603,W,1,07,1.13,1516.1,M,-17.6,M,,*5C
$GGSA,A,3,26,27,09,02,28,17,12,,,,,,1.87,1.13,1.48*07
$GPGSV,4,1,13,02,17,190,18,04,40,169,11,09,47,292,35,11,04,042,*72
$GPGSV,4,2,13,12,16,296,22,14,01,345,,15,03,234,27,17,66,051,24*71
$GPGSV,4,3,13,20,11,069,2,0,323,26,27,53,277,36,28,34,105,19*7F
$GPGSV,4,4,13,32,06,044,21*4F
$GPGLL,4539.99764,N,11102.77603,W,210417.00,A,A*72
$GPRMC,210418.00,A,4539.99756,N,11102.77591,W,0.285,,280909,,,A*68
$GPVTG,,T,,M,0.285,N,0.527,K,A*2C
$GPGGA,210418.00,4539.99756,N,11102.77591,W,1,07,1.13,151.1,M,-17.6,M,,*5A
$GPGSA,A,3,26,27,09,02,28,17,12,,,,,,1.87,1.13,1.48*07
$GPGSV,4,1,13,02,17,190,19,04,40,169,08,09,47,292,35,11,04,042,*7B
$GPGSV,4,2,13,12,16,296,22,14,01,345,,15,03,234,27,17,66,051,23*76
$GPGS,43,13,20,11,069,,26,09,323,27,27,53,277,37,28,34,105,19*7F
$GPGSV,4,4,13,32,06,044,22*4C
$GPGLL,4539.99756,N,11102.77591,W,210418.00,A,A*74

Eclipse 给我的输出是

P G 
G P
G P
G P
G G
G P
G P
G P
G P
G P
G P
G P
G P
G P
G P
G P
G P
G P

如您所见,除了第一行输出外,其他行都相差一个。

有人能告诉我我做错了什么吗?任何帮助将不胜感激。

最佳答案

您应该在if 区域添加一个continue 语句

if(ch == '\r' || ch == '\n')
{
/* your code */
continue;
}

这样当您阅读下一行时,'\r' 和 '\n' 就不会被分配到数组的开头。

关于c - C中的文件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26112224/

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