Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
const char stats[40]="****************************************"; //redacted
const char inits[40]=" ";
void IterateDay(int date);
int cInputString(int iMaxChar, char *cArr, int iValidate);
int main(int argc, char *argv[])
{
int iDays = 0;
char cInput[8];
do
{
printf("Enter the Year and Month in YYYYMM format:\n");
cInputString(6,cInput,2);
if(cInput[4] == '0')
{
switch (cInput[5])
{
case '1':
case '3':
case '5':
case '7':
case '8': iDays = 31; break;
case '4':
case '6':
case '9': iDays = 30; break;
case '2':
{
char cYear[4];
strncpy(cYear,cInput,4);
if(atoi(cYear)%4==0) iDays = 29;
else iDays = 28;
break;
}
}
}
else if(cInput[4] == '1')
{
if(cInput[5] == '0' || cInput[5] == '2') iDays = 31;
if(cInput[5] == '1') iDays = 30;
}
}
while(iDays < 28);
cInput[6] = '\0';
printf("cInput: %s\n",cInput);
char str[40]=" ";
char spac[1]=" ";
int ch;
FILE *fp;
FILE *finuse;
FILE *ftpbs;
finuse=fopen("c:\\finuse.txt", "w+");
if(finuse == NULL) printf("Inuse Write File Error: %s", strerror(errno));
fclose(finuse);
ftpbs=fopen("c:\\ftpbs.txt", "w+");
if(finuse == NULL) printf("TPBS Write File Error: %s", strerror(errno));
fclose(ftpbs);
char tim[9]=" "; //Initialize Time
strcpy(str,inits); //Initialize String
char tp[3]=" ";
char bs[5]=" ";
char to[4]=" ";
char pc[3]=" "; //Percent
char inuse[7]=" "; //Total Resources Inuse
int InStats=0; //value to indicate position inside stats loop
int i=0; //Counter
int k;
char cFilename[80];
char cPath[24] = "C:\\********************"; //redacted
char cAppend[27] = "***********************.LOG"; //redacted
for(k=1;k<=iDays;k++)
{
if(k<10)
{
snprintf(cFilename, sizeof(cFilename), "%s%s0%i %s",cPath,cInput,k,cAppend);
}
else snprintf(cFilename, sizeof(cFilename), "%s%s%i %s",cPath,cInput,k,cAppend);
printf("cInput: %s\n",cInput);
fp=fopen(cFilename, "r+");
if(fp == NULL) printf("Read File Error: %s", strerror(errno));
else
{
printf("Reading %s\n",cFilename);
printf("cInput: %s\n",cInput);
system("PAUSE");
while((ch=fgetc(fp)) != EOF)
{
if(i>11 && i<20) tim[i-12]=ch;
if((ch=='\n' || ch=='\r') && !InStats) //if hitting \n while instats TRUE
//we need to store the stats first
{
if(strcmp(str,stats)==0) InStats=1;
i=-1;
strcpy(tim," ");
strcpy(str,inits);
}
else if(i>40 && !InStats) str[i-41]=ch;
else if(InStats)
{
if(ch=='\n' && InStats<6)
{
i=-1;
strcpy(tim," ");
InStats++;
}
else if(ch!='\n' && InStats==6)
{
if(i>22 && i<25)
{
if(ch==32) InStats++;
else tp[i-23]=ch;
}
if(i>38 && i<43) bs[i-39]=ch;
if(i>53 && i<57) to[i-54]=ch;
if(i>64 && i<67) pc[i-64]=ch;
}
else if((ch=='\n' || ch=='\r') && InStats==6)
{
ftpbs=fopen("c:\\ftpbs.txt", "a+");
fprintf(ftpbs,"%s, %s, %s, %s, %s\n",tim,tp,bs,to,pc);
fflush(ftpbs);
fclose(ftpbs);
i=-1;
strcpy(tim," ");
strcpy(tp," ");
strcpy(bs," ");
strcpy(to," ");
strcpy(pc," ");
}
else if(ch=='\n' && InStats>6 && InStats < 10)
{
InStats++;
i=-1;
}
else if(ch!='\n' && InStats>9)
{
if(i>42 && i<49) inuse[i-43]=ch;
}
else if(ch=='\n' && InStats>9)
{
finuse=fopen("c:\\finuse.txt", "a+");
fprintf(finuse,"%s, %s \n",tim,inuse);
fflush(finuse);
fclose(finuse);
InStats=0;
i=-1;
}
}
i++;
}
}
fflush(stdin);
fflush(fp);
printf("%s done\n\n\n",cFilename);
printf("cInput: %s\n\n\n",cInput);
// strcpy(cInput,cInputCopy);
printf("k: %i\n",k);
printf("cAppend: %s\n",cAppend);
system("PAUSE");
fclose(fp);
}
system("PAUSE");
return 0;
}
int cInputString(int iMaxChar, char *cArr, int iValidate)
{
/* This is a character input function to limit the length of characters
inputted by the user. This is necessary because without using such a
function, user could overrun the input stack and cause errors. It'll
accept single-character inputs until it hits the number of characters
requested (iMaxChar) or until the user hits Enter. */
/* iValidate determines whether to check for input. Data is checked based on
the following table:
0: do not check data
1: verify all data is alpha (lower and upper-case letters)
2: verify all data is numeric */
/* int return is the length of the characters inputted */
fflush(stdin);
char cC;
int iCount = 0;
do
{
cC = getch();
printf ("%c",cC);
switch (iValidate)
{
case 1:
{
if((isalpha(cC))||cC==' '||cC==0xd)
{
cArr[iCount] = cC;
++iCount;
break;
}
else
{
printf("\nInvalid Entry, use only letters. Please try again: ");
break;
}
}
case 2:
{
if (!isdigit(cC))
{
printf("\nInvalid Entry, use only numbers. Please try again: ");
break;
}
else if (cC == 0xd) break;
else
{
cArr[iCount] = cC;
++iCount;
break;
}
}
default:
{
cArr[iCount] = cC;
++iCount;
break;
}
}
}
while (iCount < iMaxChar && cC != 0xd);
printf ("\n");
fflush(stdin);
return iCount;
}