Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define ncols 8 //number of columns to read
#define dt_len 10 //length of string arrays for date arrays
#define tm_len 8 //length of strings for time arrays
#define line_len 400 //length for dummy line
#define COND1 tmin<=data[i][4] && data[i][4]<=tmax &&\
tmin<=data[i][5] && data[i][5]<=tmax &&\
tmin<=data[i][6] && data[i][6]<=tmax &&\
tmin<=data[i][7] && data[i][7]<=tmax
#define COND2 data[i][4]<=tmax &&\
data[i][5]<=tmax &&\
data[i][6]<=tmax &&\
data[i][7]<=tmax
int main(void)
{
/* declare and initialise everything */
int i=0, j=0, k=0, nrows=0;
float tmin=100.0, tmax=250.0, tav=0.0, tol=0.0;
float (*data)[ncols]=NULL;
FILE *fp=NULL;
FILE *newfp=NULL;
char **date=NULL, **moment=NULL;
const char *oldname="combi1.dat";
char tempstr[6];
char newname[15];
char buff[line_len];
/* count the number of lines in the data file */
if (!(fp=fopen(oldname,"r"))) {
perror(oldname);
exit(1);
}
while (fgets(buff,sizeof buff,fp)!=NULL) {
nrows++;
}
/* allocate 2d arrays */
date=malloc(nrows*sizeof(char *));
for (i=0; i<nrows; i++) {
date[i]=malloc((dt_len+1)*sizeof(char));
}
moment=malloc(nrows*sizeof(char *));
for (i=0; i<nrows; i++) {
moment[i]=malloc((tm_len+1)*sizeof(char));
}
data=malloc(nrows*(ncols+1)*sizeof(float));
/* read the file and copy time and data values */
rewind(fp);
for (i=0; i<nrows; i++) {
fscanf(fp,"%s%s%f",date[i],moment[i],&data[i][0]);
for (j=1; j<ncols; j++) {
fscanf(fp,"%*s%*s%f",&data[i][j]);
}
}
fclose(fp);
/* ask for user info */
do {
do {
printf("\nEnter the average temperature in degC: ");
scanf("%f", &tav);
if (tav<0.0) {
printf("\nPlease enter a positive value for temperature.");
}
} while (tav<0.0);
do {
printf("Enter the +/- tolerance in degC: ");
scanf("%f", &tol);
if (tol<0.0) {
printf("\nPlease enter a positive value for the tolerance.");
}
} while (tol<0.0);
if (tol>tav) {
printf("The tolerance must be smaller than the average temperature.");
printf("\nHere 1.4");
}
} while (tol>tav);
//compute tmin and tmax
tmin=tav-tol;
tmax=tav+tol;
/* create name of new file with format: tmin-tmax.txt */
// check if the tmin needs one or two zeros infront
if (tmin<100.0) {
newname[0]='0';
if (tmin<10.0) {
newname[1]='0';
}
}
snprintf(tempstr,6,"%.1f",tmin);
strcat(newname,tempstr);
strcat(newname,"-");
// check if the tmax needs one or two zeros infront
if (tmax<100.0) {
newname[6]='0';
if (tmax<10.0) {
newname[7]='0';
}
}
snprintf(tempstr,6,"%.1f",tmax);
strcat(newname,tempstr);
strcat(newname,".txt");
/* open the new file to write the values */
if (!(newfp=fopen(newname,"w"))) {
perror(newname);
exit(1);
}
fprintf(newfp,"#Date\t\tTime\t\tForce1\tForce2\tForce3\
\tForce4\tTemp1\tTemp2\tTemp3\tTemp4");
/* copy all rising temperature data */
i=0;
do {
fprintf(newfp,"\n%s\t%s",date[i],moment[i]);
for (j=0; j<ncols; j++) {
fprintf(newfp,"\t%.2f", data[i][j]);
}
i++;
} while (COND2);
k=i;
/* scan all other rows of the data array */
for (; i<nrows; i++) { //don't initialise i
// copy all rising temperature data
if (COND1) {
fprintf(newfp,"\n%s\t%s",date[i],moment[i]);
for (j=0; j<ncols; j++) {
fprintf(newfp,"\t%.2f", data[i][j]);
}
k++;
}
}
/* copy falling temp data */
i=nrows-1; // start from the end of array
//move backwards and keep the index of the arrays till the last point where t=tmin
while (COND2) {
i--;
}
k=k+(nrows-1-i);
for (; i<nrows; i++) { //don't initialise i
fprintf(newfp,"\n%s\t%s",date[i],moment[i]);
for (j=0; j<ncols; j++) {
fprintf(newfp,"\t%.2f", data[i][j]);
}
}
/* release the memory */
free(date);
free(moment);
free(data);
fclose(newfp);
/* Print statistics */
printf("\nMinimum temperature is: %.1f\
\nMaximum temperature is: %.1f\
\n\nNumber of records read: %i\
\nNumber of records copied: %i\
\nNumber of records deleted: %i\
\n\nAll done!, running gnuplot now...\n",\
tmin, tmax, nrows, k, nrows-k);
/* run gnuplot to plot the file... */
return 0;
}