Task: Text processing_1

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int badHrs, maxBadHrs;

static double hrsTot = 0.0;
static int rdgsTot = 0;
char bhEndDate[40];

int mungeLine( char *line, int lno, FILE *fout )
{
    char date[40], *tkn;
    int   dHrs, flag, hrs2, hrs;
    double hrsSum;
    int   hrsCnt = 0;
    double avg;

    tkn = strtok(line, ".");
    if (tkn) {
        int n = sscanf(tkn, "%s %d", &date, &hrs2);
        if (n<2) {
            printf("badly formated line - %d %s\n", lno, tkn);
            return 0;
        }
        hrsSum = 0.0;
        while( tkn= strtok(NULL, ".")) {
            n = sscanf(tkn,"%d %d %d", &dHrs, &flag, &hrs);
            if (n>=2) {
                if (flag > 0) {
                    hrsSum += 1.0*hrs2 + .001*dHrs;
                    hrsCnt += 1;
                    if (maxBadHrs < badHrs) {
                        maxBadHrs = badHrs;
                        strcpy(bhEndDate, date);
                    }
                    badHrs = 0;
                }
                else {
                    badHrs += 1;
                }
                hrs2 = hrs;
            }
            else {
                printf("bad file syntax line %d: %s\n",lno, tkn);
            }
        }
        avg = (hrsCnt > 0)? hrsSum/hrsCnt : 0.0;
        fprintf(fout, "%s  Reject: %2d  Accept: %2d  Average: %7.3f\n",
                date, 24-hrsCnt, hrsCnt, hrsSum/hrsCnt);
        hrsTot += hrsSum;
        rdgsTot += hrsCnt;
    }
    return 1;
}

int main()
{
    FILE *infile, *outfile;
    int lineNo = 0;
    char line[512];
    const char *ifilename = "readings.txt";
    outfile = fopen("V0.txt", "w");

    infile = fopen(ifilename, "rb");
    if (!infile) {
        printf("Can't open %s\n", ifilename);
        exit(1);
    }
    while (NULL != fgets(line, 512, infile)) {
        lineNo += 1;
        if (0 == mungeLine(line, lineNo, outfile))
            printf("Bad line at %d",lineNo);
    }
    fclose(infile);

    fprintf(outfile, "File:     %s\n", ifilename);
    fprintf(outfile, "Total:    %.3f\n", hrsTot);
    fprintf(outfile, "Readings: %d\n", rdgsTot);
    fprintf(outfile, "Average:  %.3f\n", hrsTot/rdgsTot);
    fprintf(outfile, "\nMaximum number of consecutive bad readings is %d\n", maxBadHrs);
    fprintf(outfile, "Ends on date %s\n", bhEndDate);
    fclose(outfile);
    return 0;
}










Fortran: Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
      INTEGER Y,M,D		
      INTEGER GOOD(24)		
      REAL*8 V(24),VTOT,T	
      INTEGER NV,N,NB		
      INTEGER I,NREC,HIC	
      INTEGER BI,BN,BBI,BBN	
      CHARACTER*10 BDATE,BBDATE	
      LOGICAL INGOOD		
      INTEGER IN,MSG		
      CHARACTER*666 ACARD	
      IN = 10		
      MSG = 6		
      OPEN (IN,FILE="Readings1.txt", FORM="FORMATTED",	
     1 STATUS ="OLD",ACTION="READ")			
      NB = 0		
      NV = 0		
      VTOT = 0		
      NREC = 0		
      HIC = 0		
      INGOOD = .TRUE.	
      BBN = 0		
Chew into the file.
   10 READ (IN,11,END=100,ERR=666) L,ACARD(1:MIN(L,LEN(ACARD)))	
      NREC = NREC + 1		
   11 FORMAT (Q,A)		
      READ (ACARD,12,END=600,ERR=601) Y,M,D	
   12 FORMAT (I4,2(1X,I2))				
      READ (ACARD(11:L),*,END=600,ERR=601) (V(I),GOOD(I),I = 1,24)	
Calculations. Could use COUNT(array) and SUM(array), but each requires its own pass through the array.
   20 T = 0		
      N = 0		
      DO I = 1,24	
        IF (GOOD(I).GT.0) THEN	
          N = N + 1		
          T = T + V(I)		
          IF (.NOT.INGOOD) THEN	
            INGOOD = .TRUE.	
            IF (BN.GT.BBN) THEN	
              BBN = BN		
              BBI = BI		
              BBDATE = BDATE	
            END IF		
          END IF		
         ELSE		
          IF (INGOOD) THEN	
            INGOOD = .FALSE.	
            BDATE = ACARD(1:10)	
            BI = I		
            BN = 0		
          END IF		
          BN = BN + 1	
        END IF		
      END DO		
Commentary for the day's data..
      IF (N.LE.0) THEN	
        WRITE (MSG,21) NREC,ACARD(1:10)	
   21   FORMAT ("Record",I8," (",A,") has no good data
       ELSE			
        WRITE(MSG,22) NREC,ACARD(1:10),N,T/N	
   22   FORMAT("Record",I8," (",A,")",I3," good, average",F9.3)	
        NB = NB + 24 - N	
        NV = NV + N		
        VTOT = VTOT + T		
      END IF			
      GO TO 10		

Complaints. Should really distinguish between trouble in the date part and in the data part.
  600 WRITE (MSG,*) '"END" declared - insufficient data?'	
      GO TO 602				
  601 WRITE (MSG,*) '"ERR" declared - improper number format?'	
  602 WRITE (MSG,603) NREC,L,ACARD(1:L)	
  603 FORMAT(" Record ",I0,", length ",I0," reads ",A)	
      HIC = HIC + 1			
      IF (HIC.LE.12) GO TO 10		
      STOP "Enough distaste."		
  666 WRITE (MSG,101) NREC,"format error
      GO TO 900				

Closedown.
  100 WRITE (MSG,101) NREC,"then end-of-file"	
  101 FORMAT (" Record ",I0,": ",A)		
      WRITE (MSG,102) NV,NB,VTOT/NV		
  102 FORMAT (I8," values, ",I0," bad. Average",F9.4)	
      IF (BBN.LE.0) THEN		
        WRITE (MSG,*) "No bad value presented, so no longest run."	
       ELSE				
        WRITE (MSG,103) BBN,BBI,BBDATE	
  103   FORMAT ("Longest bad run: ",I0,", starting hour ",I0," on ",A)	
      END IF			
  900 CLOSE(IN)		
      END