[plug] C help (only slightly Linux)

John Summerfield summer at os2.ami.com.au
Tue Jul 21 00:22:06 WST 1998

On Sun, 19 Jul 1998, Shackleton, Kevin wrote:

> I tried your phone at 19:20 tonight and did not get an a reply.
> If you could steer me along that would be great.  I think it will
> probably take a week or two to get the program in reasonable form, with
> you developing and me steering, via emails.  If you can handle that . .?
> Here's a start.
> We want a program that monitors the GPS traffic and prompts you for an
> input every 1 km (there's 1852 m per minute of angle by the way).  We
> could input the data as a line of text, but really it's about five or
> six items, like "condition score", which have allowable inputs  ({0..4}
> in this case) plus probably a comment line.
> I don't know if window management (using Borland's gotoxy() or Turbo
> Vision) is really worthwhile.
> So there needs to be a startup situation which sets a "last location"
> position, a loop that computes current distance to last location, and a
> data input part.
> I wrote this to read through the data file:
> #include <stdio.h>
> #include <string.h>
> int main() {
>   char string[80];
>   char header[6];
>   do {
>     gets(string);

Don't use gets - it overruns the input area if the input's too long.
Something like this is safer:
  while (fgets(buff,sizeof(buff),stdin)) {
     if (buff[strlen(buff)-1] == 10) (buff[strlen(buff)-1] =0);
          printf("%d: %s\n",count,buff);
     lastchar = buff[strlen(buff)-1];
     if (strlen(buff)) 
        ptok = strtok(buff2," ");
     } /* endif */
     if (endControlStatements) break;
   } /* endwhile */

fgets stops when it gets to \n or it's filled the buffer.

strtok's good for splitting data: I want to split on spaces (tough if the
user uses tabs). 

>     strncpy(header,string,6);
>     if(strcmp(header,"$GPRMC"))

You can use strncmp to compare the first few chars.

>       printf(string);
>   } while (!eof());
>   return 0;
> }
> However this doesn't work undet linux (core dump).  I assume that it's
> because there's no null termination on header.

I'm compiling my latest gem thus:
gcc -pedantic -x c++  -g  -ggdb  -lefence  -o Z z.c 2>&1 || exit

-lefence links with electic fence which (I discovered today) checks for
accesses to unallocated storage.

 This allows me to use gdb to discover where mine breaks.

John Summerfield whos still not found his sig.

More information about the plug mailing list