This is an assignment from high school where we were given delimited text file and were asked to compute statistics on the data using a C program and store the results in a database.

met_dat.c

/***************************************************************
*
*	MetDat
*	Stephen Jenkins (c) 2012
*
*	Reads meteorological data from a text file and computes
*	some information about it
*
****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define DATABASE_SIZE 21

int main(void){
	//declare the file pointer
	FILE* my_file = NULL;

	//declare the rest of the variables
	int counter = 0;
	int counter_2 = 0;
	int database[4][DATABASE_SIZE];
	int temp;
	int max;
	int min;
	char path[256];
	char buffer[127];
	char dump;
	float average;

	//print the program name and purpose
	system("clear");
	printf("MetDat\n");
	printf("Stephen Jenkins (c) 2012\n");
	printf("\nReads meteorlogical data from a text file\n");
	printf("and computes some information on it.\n");

	//get the file scanned into the database
	do{
		//scan for the path
		printf("\nEnter the path to the text file: ");
		fgets(path, 256, stdin);

		//get rid of the new line from fgets
		temp = strlen(path);
		path[temp-1] = '\0';

		//continue only if the path is sane
		if(strlen(path) > 1 && isdigit(path[0]) != 1){
			//open the file
			my_file = fopen(path, "rt");

			//get the data from the text file
			fscanf(my_file, "%[^\n]", buffer); //ignore the first line...
			fgetc(my_file);
			for(counter = 0; counter < DATABASE_SIZE; counter ++){
				for(counter_2 = 0; counter_2 < 3; counter_2 ++){
					if(!feof(my_file)){
						fscanf(my_file, "%[^\t]", buffer);
						fgetc(my_file);
						sscanf(buffer, "%d", &database[counter_2][counter]);
//						printf("\nscanned in: %d\n", database[counter_2][counter]);
					}
				}
				if(!feof(my_file)){
					fgets(buffer, 127, my_file);
					sscanf(buffer, "%d", &database[counter_2][counter]);
//					printf("\nscanned in: %d\n", database[counter_2][counter]);
				}
			}

			//check for an error from reading the file
			if(my_file == NULL){
				printf("\nERROR: FILE DOES NOT EXIST\n");
				clearerr(my_file);
			}

			//close the file
			fclose(my_file);
		}
	}while(my_file == NULL);

	printf("\nData had been recovered from the file sucessfully!\n");
	printf("Press any key to continue...");
	getchar();

	temp = 0;
	for(counter = 0; counter < DATABASE_SIZE; counter ++){
		temp += database[2][counter];
	}
	average = 1.0*temp/DATABASE_SIZE;
	printf("\nThe average wind speed for all the stations is: %f\n", average);
	temp = 0;
	for(counter = 0; counter < DATABASE_SIZE; counter ++){
		if(database[2][counter] > temp) temp = database[2][counter];
	}
	printf("\nThe maximum wind speed across all of the stations is: %d\n", temp);
	max = temp;
	temp = 32767;
	for(counter = 0; counter < DATABASE_SIZE; counter ++){
		if(database[2][counter] < temp) temp = database[2][counter];
	}
	min = temp;
	printf("\nThe minimum wind speed across all of the stations is: %d\n", temp);

	//save everything to a new text file thing
	sprintf(path, "./Lab_9_Results.dat");
	my_file = fopen(path, "wt");
	if(my_file == NULL){
		printf("\nCRITICAL ERROR: NOW EXITING\n");
		clearerr(my_file);
		fclose(my_file);
		clearerr(my_file);
		exit(-1);
	}

	fprintf(my_file, "Station\t\tDay\t\tWind Speeds (knots)\tTemperature (deg C)\n");
	for(counter_2 = 0; counter_2 < DATABASE_SIZE; counter_2++){
		for(counter = 0; counter < 4; counter ++){
			fprintf(my_file, "%d\t\t", database[counter][counter_2]);
			if(counter == 2)fprintf(my_file, "\t");
		}
		fprintf(my_file, "\n");
	}
	fprintf(my_file, "Average Wind Speed for All Stations = %f\n", average);
	fprintf(my_file, "Maximum Wind Speed for All Stations = %d\n", max);
	fprintf(my_file, "Minimum Wind Speed for All Stations = %d\n", min);

	printf("\nEverything was saved sucessfully!\n");
	printf("Press any key to quit...");

	getchar();

	fclose(my_file);

	//return 0
	return(0);
}

data.txt

Station	Day	Wind Speed (knots)	Tempurature (deg C)
2001	10	11	30
2001	11	5	22
2001	12	18	25
2001	13	16	26
2001	14	14	26
2001	15	2	25
2001	16	14	22
3345	10	8	29
3345	11	5	23
3345	12	12	23
3345	13	14	24
3345	14	10	24
3345	15	9	22
3345	16	9	20
3819	10	17	27
3819	11	20	21
3819	12	22	21
3819	13	18	22
3819	14	15	22
3819	15	9	19
3819	16	12	18
Log in to comment