[英]File Parsing in C
I am trying to write a basic GPS Parser. 我正在尝试编写基本的GPS解析器。 I am taking in data one character at a time and putting it into a character array till CRLF is not detected. 我一次要获取一个字符的数据,然后将其放入一个字符数组,直到未检测到CRLF。 However it seems my character array is one off at a time. 但是,似乎我的字符数组一次关闭了。 Also letters[100] because the max size is known to not exceed 100. Plus I am filling in garbage character ">" since I know it wont be a part of the GPS packet. 也可以是字母[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;
}
This is the GPS text file which I am including the data from 这是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
The output Eclipse gives me is 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
As you can see apart from the first output the other lines are all off by one. 如您所见,除了第一个输出以外,其他所有行都相距一。
Can someone tell me what am I doing wrong? 有人可以告诉我我在做什么错吗? Any help will be appreciated. 任何帮助将不胜感激。
You should add a continue
statement in the if
region 您应该在if
区域添加一个continue
语句
if(ch == '\r' || ch == '\n')
{
/* your code */
continue;
}
So that '\\r' and '\\n' won't be assigned to the beginning of your array when you read your next line. 因此,当您读取下一行时,'\\ r'和'\\ n'不会分配给数组的开头。
The next line begins at index 1 that is: letters[1] instead of letters[0]. 下一行从索引1开始,即:字母[1]而不是字母[0]。
int pointer = 0;
while( ( ch = fgetc(fp) ) != EOF )
{
if(ch == '\r' || ch == '\n')
{
printf("%c %c", letters[2], letters[3]);
pointer =0;
for( i=0; i<100; i++)
{
letters[i] ='>';
}
}
letters[pointer] = ch;
if(ch != '\r' || ch != '\n')
{
pointer++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.