Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
const char stats[45]="UNRELATED MESSAGE THAT I AM TESTING FOR";
const char inits[45]=" ";
const char triu[20]="L RESOURCES I";
const char tspa[20]=" ";
void IterateDay(int date);
int cInputString(int iMaxChar, char *cArr, int iValidate);
int main(int argc, char *argv[])
{
printf("Welcome to TPBS Report Generator v0.1\n");
printf("Program currently only looks at a single month at a time\n\n");
int iDays = 0;
// int iDays = 1; //For testing, comment do/while and uncomment this line
char cYear[6];
char cMo[4];
int iLnCt = 0;
int iChVal = 0;
printf("Enter the Year in YYYY format:\n");
cInputString(4,cYear,2);
do
{
printf("Enter the Month in MM format:\n");
cInputString(2,cMo,2);
if(cMo[0] == '0')
{
switch (cMo[1])
{
case '1':
case '3':
case '5':
case '7':
case '8': iDays = 31; break;
case '4':
case '6':
case '9': iDays = 30; break;
case '2':
{
if(atoi(cYear)%4==0) iDays = 29;
else iDays = 28;
break;
}
}
}
else if(cMo[0] == '1')
{
if(cMo[1] == '0' || cMo[1] == '2') iDays = 31;
if(cMo[1] == '1') iDays = 30;
}
}
while(iDays < 28);
cYear[4] = '\0';
cMo[2] = '\0';
char cInput[8];
strcpy(cInput,cYear);
strcat(cInput,cMo);
// printf("cInput: %s\n",cInput);
// system("PAUSE");
char str[200]=" ";
char spac[1]=" ";
int ch;
FILE *fp;
FILE *finuse;
FILE *ftpbs;
char cOutInuse[80];
char cOutTPBS[80];
snprintf(cOutInuse, sizeof(cOutInuse), "c:\\%s inuse.csv",cInput);
snprintf(cOutTPBS, sizeof(cOutTPBS), "c:\\%s TPBS.csv",cInput);
finuse=fopen(cOutInuse, "w+");
if(finuse == NULL) printf("Inuse Write File Error: %s\n", strerror(errno));
else
{
fprintf(finuse,"Date/Time,Total Resources Inuse\n");
fflush(finuse);
}
fclose(finuse);
ftpbs=fopen(cOutTPBS, "w+");
if(finuse == NULL) printf("TPBS Write File Error: %s\n", strerror(errno));
fclose(ftpbs);
char tim[10]=" ";
strcpy(str,inits);
char tp[6]=" ";
char bs[6]=" ";
char to[6]=" ";
char pc[6]=" ";
char inuse[8]=" "; //Total Resources Inuse
int iSpace = 0;
int iTotal = 0;
int InStats=0; //value to indicate position inside stats loop
int i=0; //Counter
int k;
char cInputFile[80];
char cPath[25] = "C:\\*****************\\"; //redacted
char cAppend[28] = "**********************.LOG"; //redacted
for(k=1;k<=iDays;k++)
{
if(k<10)
{
snprintf(cInputFile, sizeof(cInputFile), "%s%s0%i %s",cPath,cInput,k,cAppend);
}
else snprintf(cInputFile, sizeof(cInputFile), "%s%s%i %s",cPath,cInput,k,cAppend);
printf("cInput: %s\n",cInput);
fp=fopen(cInputFile, "r+");
// fp=fopen("C:\\test.txt", "r+");
if(fp == NULL) printf("Read File Error: %s", strerror(errno));
else
{
printf("Reading %s\n",cInputFile);
// 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;
iChVal = 0;
// strcpy(tim," ");
strcpy(str,inits);
}
else if(i>40 && !InStats) str[i-41]=ch;
else if(InStats)
{
if(i>24 && i<39)
{
str[i-25]=ch;
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
}
if(i==23)
{
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
// printf("i = 23");
if(ch>=49)
{
// printf("ch = 49");
iSpace++;
tp[i-23]=ch;
}
else
{
// printf("ch = %i",ch);
}
}
else if(i==24)
{
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
// printf("time = %s",tim);
// printf("iSpace = %i\n",iSpace);
// printf("ch = %i\n",ch);
// if(ch>=58 && ch<=123 && iSpace==1)
// {
iSpace++;
tp[i-23]=ch;
tp[i-22]='\0';
// printf("tp = %s\n",tp);
// }
/* else
{
iSpace=0;
tp[i-23]='\0';
}*/
// system("PAUSE");
}
else if(i==38)
{
str[13]='\0';
if(strcmp(str,triu)==0) iTotal=1;
if(strcmp(str,tspa)==0)
{
iSpace++;
// printf("str: %s\n",str);
// printf("iSpace = %i\n",iSpace);
// printf("iTotal = %i\n",iTotal);
// system("PAUSE");
// bs[i-39]==ch;
}
}
else if(i>38 && i<43 && iSpace==3) bs[i-39]=ch;
else if(i>38 && i<43 && iSpace!=3) iSpace=0;
else if(i==43 && iSpace==3) bs[i-39]='\0';
else if(i>53 && i<57 && iSpace==3) to[i-54]=ch;
else if(i==57 && iSpace==3) to[i-54]='\0';
else if(i>64 && i<67 && iSpace==3) pc[i-64]=ch;
else if(i>=43 && i<=47 && iTotal==1) inuse[i-43]=ch;
else if(i==48 && iTotal==1)
{
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
// printf("Yay!\niSpace = %i\n",iSpace);
// printf("iTotal = %i",iTotal);
// system("PAUSE");
inuse[i-43]=ch;
inuse[i-42]='\0';
finuse=fopen(cOutInuse, "a+");
fprintf(finuse,"%s/%i/%s %s,%s\n",cMo,k,cYear,tim,inuse);
fflush(finuse);
fclose(finuse);
// printf("%s/%i/%s %s,%s\n",cMo,k,cYear,tim,inuse);
InStats=0;
iTotal=0;
i=-1;
}
else if(i==67 && iSpace==3)
{
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
pc[i-64]=ch;
pc[i-63]='\0';
ftpbs=fopen(cOutTPBS, "a+");
fprintf(ftpbs,"%s/%i/%s %s,%s,%s,%s,%s\n",cMo,k,cYear,tim,tp,bs,to,pc);
fflush(ftpbs);
fclose(ftpbs);
// printf("%s/%i/%s %s,%s,%s,%s,%s\n",cMo,k,cYear,tim,tp,bs,to,pc);
// printf("%s\n",pc);
iSpace=0;
i=-1;
}
else if(ch=='\n' || ch=='\r')
{
// printf("i: %i\n",i);
// printf("ch: %i\n",ch);
// system("PAUSE");
iSpace=0;
iTotal=0;
i=-1;
ch=0;
}
}
i++;
}
}
fflush(stdin);
fflush(fp);
printf("%s done\n\n\n",cInputFile);
// printf("cInput: %s\n\n\n",cInput);
// strcpy(cInput,cInputCopy);
// printf("k: %i\n",k);
// printf("cAppend: %s\n",cAppend);
// system("PAUSE");
fclose(fp);
}
printf("Files written:\n%s\n%s\n",cOutInuse,cOutTPBS);
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;
}
That 1Z line isn't getting picked up - it should, but it isn't. When I go over the entire file, it's simply missing from my output. When I go over just this small test file, it puts it in the output, but for some reason the *time* changes to something that shouldn't even be possible (00:00:15 - the same time as the first iteration in my full file).