Thread: Brand new to C need favor

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Brand new to C need favor

    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 */

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    I read the homework policy and realize this sounds like it might be homework, however it is for research and not at all for a grade. I would appreciate all and any help possible or advice about where to turn to.
    Thank you

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need "int main()" and a "{" before the "gpsinfile ... " line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you have a comment which says "end of main", but there is no obvious sign of a main at the start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Okay, so when I fixed the "int main() {" problem and tried to compile I got many errors of the same type.

    What is undeclared and how to fix?

    gpsinfile’ undeclared (first use in this function)
    firstattempt.c:5: error: (Each undeclared identifier is reported only once
    firstattempt.c:5: error: for each function it appears in.)
    firstattempt.c:6: error: ‘gpsoutfile’ undeclared (first use in this function)
    firstattempt.c:9: error: ‘numLinesRead’ undeclared (first use in this function)
    firstattempt.c:13: error: ‘charRead’ undeclared (first use in this function)
    firstattempt.c:15: error: ‘i’ undeclared (first use in this function)
    firstattempt.c:17: error: ‘stringRead’ undeclared (first use in this function)
    firstattempt.c:24: error: ‘CR’ undeclared (first use in this function)
    firstattempt.c:31: error: ‘j’ undeclared (first use in this function)
    firstattempt.c:32: error: ‘pChar’ undeclared (first use in this function)
    firstattempt.c:33: error: ‘COMMA’ undeclared (first use in this function)
    firstattempt.c:34: error: ‘tempString’ undeclared (first use in this function)
    firstattempt.c:69: error: ‘k’ undeclared (first use in this function)
    firstattempt.c:71: error: ‘timeString’ undeclared (first use in this function)
    firstattempt.c:75: error: ‘lastCommaPosition’ undeclared (first use in this func tion)
    firstattempt.c:77: error: ‘utcTime’ undeclared (first use in this function)
    firstattempt.c:78: error: ‘utcHour’ undeclared (first use in this function)
    firstattempt.c:79: error: ‘utcMinutes’ undeclared (first use in this function)
    firstattempt.c:80: error: ‘utcSeconds’ undeclared (first use in this function)
    firstattempt.c:82: error: ‘estHour’ undeclared (first use in this function)
    firstattempt.c:84: error: ‘estMinutes’ undeclared (first use in this function)
    firstattempt.c:85: error: ‘estSeconds’ undeclared (first use in this function)
    firstattempt.c:95: error: ‘latitudeString’ undeclared (first use in this functio n)
    firstattempt.c:102: error: ‘latitude’ undeclared (first use in this function)
    firstattempt.c:103: error: ‘latDegrees’ undeclared (first use in this function)
    firstattempt.c:104: error: ‘latMinutes’ undeclared (first use in this function)
    firstattempt.c:112: error: ‘latitudeCardinalString’ undeclared (first use in thi s function)
    firstattempt.c:125: error: ‘longitudeString’ undeclared (first use in this funct ion)
    firstattempt.c:132: error: ‘longitude’ undeclared (first use in this function)
    firstattempt.c:133: error: ‘longDegrees’ undeclared (first use in this function)
    firstattempt.c:134: error: ‘longMinutes’ undeclared (first use in this function)
    wademat@tux181:~/Desktop/Code>
    Last edited by dontknowc; 09-21-2007 at 10:07 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from the FILE *gpsinfile and FILE *gpsoutfile that you probably should have added, I expect the code that you copied from contains declarations of those longitude, etc, variables, so maybe you just need to copy a bit more from the original.

    But it's very hard to say - it is not obvious what those things are from just looking at your code briefly.

    Further, there are plenty of other things that seem to be wrong in your code. kbhit() is probably not something you want to do in your code, if you are reading from a file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RAND() Does it favor the lower number?
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2006, 02:38 PM
  2. A huge favor to ask
    By Doomkiller in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2005, 08:56 PM
  3. Asking a favor of anyone with a GeForce 3
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-23-2005, 09:49 PM
  4. Could someone do me a REALLY quick favor
    By DarkViper in forum Windows Programming
    Replies: 1
    Last Post: 08-10-2003, 04:50 AM
  5. Need a favor!!!
    By Nick Dillon in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2002, 05:40 PM