I am trying to write a basic GPS Parser. I am taking in data one character at a time and putting it into a character array till CRLF is not detected. 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.
#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
$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
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(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.
The next line begins at index 1 that is: letters[1] instead of letters[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++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.