I have never written or learned any C programming however for a project I am working on I need parse a file and was told to do it in c. I found some code that does what I want but reads it from a serial port so I attempted to make small changes to make it read from a file.

Problem: Need to take a text file with lines of GPS messages and parse it so that certain information is extracted and send to a new text file. I apologize for my incompetence. Please help.

When I tried to compile as is I got many errors and being about 1 week into c it was over my head:
firstattempt.c:4: warning: data definition has no type or storage class
firstattempt.c:4: warning: initialization makes integer from pointer without a cast
firstattempt.c:4: error: initializer element is not constant
firstattempt.c:5: warning: data definition has no type or storage class
firstattempt.c:5: warning: initialization makes integer from pointer without a cast
firstattempt.c:5: error: initializer element is not constant
firstattempt.c:8: warning: data definition has no type or storage class
firstattempt.c:10: error: expected declaration specifiers or ‘...’ before string constant
firstattempt.c:10: warning: data definition has no type or storage class
firstattempt.c:10: error: conflicting types for ‘printf’
firstattempt.c:10: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
firstattempt.c:11: error: expected identifier or ‘(’ before ‘do’
firstattempt.c:142: error: expected identifier or ‘(’ before ‘while’
firstattempt.c:144: error: expected declaration specifiers or ‘...’ before string constant
firstattempt.c:144: warning: data definition has no type or storage class
firstattempt.c:144: error: conflicting types for ‘printf’
firstattempt.c:144: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
firstattempt.c:145: warning: data definition has no type or storage class
firstattempt.c:145: warning: parameter names (without types) in function declaration
firstattempt.c:146: warning: data definition has no type or storage class
firstattempt.c:146: warning: parameter names (without types) in function declaration
firstattempt.c:147: error: expected declaration specifiers or ‘...’ before string constant
firstattempt.c:147: warning: data definition has no type or storage class
firstattempt.c:147: error: conflicting types for ‘printf’
firstattempt.c:147: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
firstattempt.c:148: error: expected identifier or ‘(’ before ‘return’
firstattempt.c:150: error: expected identifier or ‘(’ before ‘}’ token




Below is the code:
Code:
#include <stdio.h>
#include <stdlib.h>

gpsinfile = fopen("gpsData.txt", "r");
gpsoutfile = fopen("gpsParsedData.txt", "w");


  numLinesRead = 0;

  printf("Entering while loop...\n");
  do {
      charRead = gpsinfile();  	/* read char from text file */
      if(charRead == '$') {     /* GPS messages start with $ char */
	  i = 0;
	  numLinesRead++;
	  stringRead[i] = charRead;
	  do {
	     charRead = com_rx();
	     if( (charRead != '\0') && (isalnum(charRead) ||  isspace(charRead) || ispunct(charRead)) ) {
		i++;
		stringRead[i] = charRead;
	     }
	  } while(charRead != CR);

	  /* By this point, a complete GPS string has been read so save it to file */
	  /* Append the null terminator to the string read */
	  stringRead[i+1] = '\0';

	  /* Analyze string that we collected */
	  j = 0;
	  pChar = stringRead;
	  while(*(pChar+j) != COMMA) {
	       tempString[j] = *(pChar+j);
	       j++;
	  }
	  tempString[j] = '\0';

	  /* Check if string we collected is the $GPGGA message */
	  if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] == 'A') {
	      /*
		 Found GPGGA string.  It has 14 commas total.  Its NMEA sentence structure is:

		 $GPGAA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC
		 |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
		 0   	   1         2         3         4         5         6         7
		 0123456789012345678901234567890123456789012345678901234567890123456789012

		 where:

		 GPGAA		: GPS fixed data identifier
		 hhmmss.ss	: Coordinated Universal Time (UTC), also known as GMT
		 ddmm.mmmm,n	: Latitude in degrees, minutes and cardinal sign
		 dddmm.mmmm,e	: Longitude in degrees, minutes and cardinal sign
		 q		: Quality of fix.  1 = there is a fix
		 ss		: Number of satellites being used
		 y.y		: Horizontal dilution of precision
		 a.a,M		: GPS antenna altitude in meters
		 g.g,M		: geoidal separation in meters
		 t.t		: Age of the defferential correction data
		 iiii		: Deferential station's ID
		 *CC		: checksum for the sentence
	      */

	      pChar = stringRead;

	      /* Get UTC time */
	      j = 7;  /* start of time field */
	      k = 0;
	      while(*(pChar+j) != COMMA) {
		   timeString[k] = *(pChar+j);
		   j++;
		   k++;
	      }
	      lastCommaPosition = j;
	      timeString[k] = '\0';
	      sscanf(timeString, "%ld", &utcTime);
	      utcHour = (utcTime/10000);   /* extract Hours from long */
	      utcMinutes = (utcTime - (utcHour*10000))/100;  /* extract minutes from long */
	      utcSeconds = utcTime - (utcHour*10000) - (utcMinutes*100); /* extract seconds from long */

	      if(utcHour >= 4 && utcHour <= 23) estHour = utcHour - 4;
		else estHour = utcHour + 20;
	      estMinutes = utcMinutes;
	      estSeconds = utcSeconds;

	      /* NB: %02ld formats long to print 2 chars wide, padding with 0 if necessary */
	      fprintf(gpsoutfile,"%02ld:%02ld:%02ld UTC = %02ld:%02ld:%02ld EST", utcHour, utcMinutes, utcSeconds, estHour, estMinutes, estSeconds);

	      /* Get lattitude: ddmm.mmmm */
	      pChar = stringRead;
	      j = lastCommaPosition + 1;
	      k = 0;
	      while(*(pChar+j) != COMMA) {
		   latitudeString[k] = *(pChar+j);
		   j++;
		   k++;
	      }
	      lastCommaPosition = j;
	      latitudeString[k] = '\0';

	      sscanf(latitudeString, "%f", &latitude);
	      latDegrees = (int)(latitude/100);
	      latMinutes = (float)(latitude - latDegrees*100);
	      fprintf(gpsoutfile,"\t%02d DEG\t%2.4f MIN", latDegrees, latMinutes);

	      /* Get lattitude Cardinal direction */
	      pChar = stringRead;
	      j = lastCommaPosition + 1;
	      k = 0;
	      while(*(pChar+j) != COMMA) {
		   latitudeCardinalString[k] = *(pChar+j);
		   j++;
		   k++;
	      }
	      lastCommaPosition = j;
	      latitudeCardinalString[k] = '\0';
	      fprintf(gpsoutfile," %s", latitudeCardinalString);

	      /* Get longitude: dddmm.mmmm */
	      pChar = stringRead;
	      j = lastCommaPosition + 1;
	      k = 0;
	      while(*(pChar+j) != COMMA) {
		   longitudeString[k] = *(pChar+j);
		   j++;
		   k++;
	      }
	      lastCommaPosition = j;
	      longitudeString[k] = '\0';

	      sscanf(longitudeString, "%f", &longitude);
	      longDegrees = (int)(longitude/100);
	      longMinutes = (float)(longitude - longDegrees*100);
	      fprintf(gpsoutfile,"\t%03d DEG\t%2.4f MIN", longDegrees, longMinutes);

	      fprintf(gpsoutfile,"\n");
	  } /* else not a GPGGA sentence */

	  printf("%d: (%d) %s\n", numLinesRead, i, stringRead);

      } /* otherwise not a $ character... so loop back until one arrives */
  } while(!kbhit());

  printf("Exiting...");
  fclose(gpsinfile);
  fclose(gpsoutfile);
  printf("done\n");
  return (0);

} /* end of main */