Code:
#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>
#include <string.h>
#include <time.h>
/*
NOT- Name of Transaction
NOA- Name of accounts
TST- Transaction Submit Time
ActualAB- Actul Acount Balanse
AfterTAB- AccountBalanceAfterTransactions
PNameF- PersonNameFrom
PNameT- PersonNameTo
*/
typedef struct NOT {
char PNameF[100];
char PNameT[100];
int Amount;
// int TST;
char TST[100];
}rec;
typedef struct NOA{
char *PName; //[100]; requires strcpy for ptr.
int ActulAB;
}roc;
typedef struct NOA1{
char PName[100];
int AfterTAB;
}cor;
int main (int argc, const char **argv)
{
if (argc < 2)
{
printf("no file to open %s\n", argv[1]);
return -1;
}
FILE *fp;
FILE *results;
if( ( fp = fopen(argv[1], "r")) == NULL)
{
printf("Could not open file\n");
return -1;
}
int count, tracker = 0;
int len;
char buff[1024];
char name[50];
int num;
int recCount = 0, holdingAccount = 0;
fgets(buff, sizeof buff, fp);
sscanf(buff, "%d", &count);
roc rocs[count]; // make array size indicated in file.
cor cors[count];
recCount = count;
// USING FGETS AND SSCANF
// tracker actaully kicks it out, check for null is just cuz
while ( fgets(buff, sizeof buff, fp) != NULL && tracker < count)
{
//sscanf(buff,"%s %d", rocs[tracker].PName, &rocs[tracker].ActulAB);
sscanf(buff,"%s %d", name, &num);
rocs[tracker].PName = strdup(name);
strcpy(cors[tracker].PName, name);
rocs[tracker].ActulAB = num;
cors[tracker].AfterTAB = num;
tracker++;
}
sscanf(buff, "%d", &count); // gets the next number out of file
tracker = 0;
// make array size indicated in file.
// add extras to it half of what is in the frist part of file
// to give room to add the transactions to it between two people.
// one more for good measure.
rec myrec[count + (recCount/2)+1];
holdingAccount = count;
while ( fgets(buff, sizeof buff, fp) != NULL && tracker < count)
{
sscanf(buff, "%s%s%d%s",
myrec[tracker].PNameF, myrec[tracker].PNameT,
&myrec[tracker].Amount, myrec[tracker].TST);
tracker++;
}
// working with rocs and myacc struct arrays
int twos = 0;
time_t mytime;
mytime = time(NULL);
char whatTime[100];
strcpy(whatTime, ctime(&mytime) );
for ( unsigned int a = 0; a < sizeof(rocs)/sizeof(rocs[0])/2; a++)
{
//printf("hello twos %d \nholding account %d\n", twos, holdingAccount);
// taking from adding to NOT
// arrays require strcpy no direct assingments
strcpy(myrec[holdingAccount].PNameF, rocs[twos].PName);
strcpy(myrec[holdingAccount].PNameT, rocs[twos+1].PName);
//update person getting money account
// rocs2[twos+1].ActulAB += rocs2[twos].ActulAB - 1;
cors[twos+1].AfterTAB += rocs[twos].ActulAB - 1;
//record transfer of money to transaction
myrec[holdingAccount].Amount = rocs[twos].ActulAB -1;
//remove everything but a dollar
// amount equals amount minus amount - 1
// rocs2[twos].ActulAB = rocs2[twos].ActulAB - ( rocs2[twos].ActulAB - 1 );
cors[twos].AfterTAB = rocs[twos].ActulAB - ( rocs[twos].ActulAB - 1 );
//timeatamp
strcpy(myrec[holdingAccount].TST,ctime(&mytime));
holdingAccount++;
twos += 2;
}
//clean up names
for (unsigned int a = 0; a < sizeof(rocs)/sizeof(rocs[0]); a++)
{
len = strlen(cors[a].PName);
cors[a].PName[len+1] = '\0';
}
/*
for (unsigned int a = 0; a < sizeof(rocs)/sizeof(rocs[0]); a++)
printf("%s %d\n",rocs[a].PName, rocs[a].ActulAB );
printf("Accounts updated\nName\tAmount\n");
for (unsigned int i = 0; i < sizeof(cors)/sizeof(cors[0]); i++)
printf("%s\t%d\n",cors[i].PName, cors[i].AfterTAB );
printf("\n");
for ( /*unsigned */// int a = 0; a < holdingAccount /* sizeof(myrec)/sizeof(myrec[0]) */; a++)
//{
//printf("%d: %s %s %d %s\n",a+1, myrec[a].PNameF, myrec[a].PNameT, myrec[a].Amount, myrec[a].TST );
//}
printf("\n");
printf("\n\n USING FSCANF next \n\n\n");
//**************************************************************//
// USING FSCANF
//reset stream
fseek(fp, 0, SEEK_SET);
count = 0;
tracker = 0;
fscanf(fp, "%d", &count);
roc rocs2[count];
cor cors2[count];
recCount = count;
printf("count %d\n", count);
// fill both structs ahead of time, update later.
while (fscanf(fp, "%s %d", name, &num) == 2 && tracker < count)
{
rocs2[tracker].PName = strdup(name);
strcpy(cors2[tracker].PName, name);
rocs2[tracker].ActulAB = num;
cors2[tracker].AfterTAB = num;
tracker++;
len = ftell(fp); // where it left off last.
}
// reset to get the next number
//kick it back some in other words
//to get that number
fseek(fp, len, SEEK_SET);
// printf("\n\nnext 2\n\n\n");
tracker = 0;
count = 0;
fscanf(fp, "%d", &count);
rec myrec2[count + (recCount /2)+1];
//printf(" myrec2 count %d\n", count);
holdingAccount = count;
twos = 0;
while ( fscanf(fp, "%99s%99s%d%s", myrec2[tracker].PNameF, myrec2[tracker].PNameT,
&myrec2[tracker].Amount, myrec2[tracker].TST) == 4 && tracker < count )
{
tracker++;
}
/*
//puting the updated accounts into NAO1 cor
for ( unsigned int a = 0; a < sizeof(rocs2)/sizeof(rocs2[0])/2; a++)
{
//printf("hello twos %d \nholding account %d\n", twos, holdingAccount);
// taking from adding to NOT
//add to end of file,
//holdingAccount has number where it left off
//using sets of two
//first person out of two gives
strcpy(myrec2[holdingAccount].PNameF, rocs2[twos].PName);
//second person out of two gets
strcpy(myrec2[holdingAccount].PNameT, rocs2[twos+1].PName);
//update getter with givers money - 1
//cuz we don't want to leave that person
//broke ass broke.
cors2[twos+1].AfterTAB += rocs2[twos].ActulAB - 1;
//record transfer of money to transaction
//take everything but a dollar
myrec2[holdingAccount].Amount = rocs2[twos].ActulAB -1;
//remove everything but a dollar
cors2[twos].AfterTAB = rocs2[twos].ActulAB - ( rocs2[twos].ActulAB - 1 );
//timeatamp it is using a char not an int in
//this code. Linux system date / time
strcpy(myrec2[holdingAccount].TST,ctime(&mytime));
holdingAccount++;
twos += 2;
}
*/
count = 0;
int firstPerson = 0, secondPerson = 0, matchFristP = 0; //, matchSecondP =0;
// myrec2 rocs2 cors2
// finding giving first, then find person giving to next
// then complete transaction. go to next
//start over with the total amount of
//transactions.
while (count < holdingAccount) // control loop
{
// goes through all records until it finds a match
//of first person.
while (firstPerson < holdingAccount)
{ // frist person found holding array matches name in records array
if ( (strcmp(rocs2[firstPerson].PName, myrec2[matchFristP].PNameF)) == 0)
{ //search
printf("rocs2[firstPerson].PName %s\n, myrec2[matchFristP].PNameF %s\n",
rocs2[firstPerson].PName, myrec2[matchFristP].PNameF);
while (secondPerson < recCount )
{ //find second persons name in holding array that matches in the same element of account array
// if match then both names have been found in same element of array.
if ( (strcmp(rocs2[secondPerson].PName, myrec2[matchFristP].PNameT) ) == 0)
{ printf("rocs2[secondPerson].PName %s\n, myrec2[matchFristP].PNameT %s\n",
rocs2[secondPerson].PName, myrec2[matchFristP].PNameT);
printf("amount to be transfered %d\n", myrec2[matchFristP].Amount);
//exit(0);
//take care of transaction in the second holding array.
//that is a direct copy of frist holding array
// to keep balances correct. ( ? )
cors2[firstPerson].AfterTAB -= myrec2[matchFristP].Amount;
cors2[secondPerson].AfterTAB += myrec2[matchFristP].Amount;
printf("firstPerson %s, firstPerson amount in account %d\n"
"secondPerson %s amount in account %d\n"
"amount transfured %d\n",
cors2[firstPerson].PName, cors2[firstPerson].AfterTAB,
cors2[secondPerson].PName, cors2[secondPerson].AfterTAB ,
myrec2[matchFristP].Amount);
secondPerson = 0; //reset
firstPerson++; // go to next person in holding array
//to check it in the
break;
}
secondPerson++;
printf("in second loop firstPerson = %d\n", firstPerson);
} // end 3rd while loop
matchFristP = 0; //reset
}
matchFristP++;
} // end 2nd while loop
count++;
// printf("while looping count = %d\n", count);
} // end while loop
if ( (results = fopen("results", "w")) == NULL)
{
printf("could not open results file\n");
return -1;
}
/*
printf("Name Amount\n");
for (unsigned int i = 0; i < sizeof(rocs2)/sizeof(rocs2[0]); i++)
printf("i= %d %s : %d\n",i, rocs2[i].PName, rocs2[i].ActulAB );
printf("update account\n");
printf("ID: Name Amount\n");
for (unsigned int i = 0; i < sizeof(cors2)/sizeof(cors2[0]); i++)
printf("%d %s %d\n",i, cors2[i].PName, cors2[i].AfterTAB );
*/
printf("Writing to: updating %ld files \n", sizeof(cors2)/sizeof(cors2[0]));
//for (unsigned int i = 0; sizeof(cors2)/sizeof(cors2[0]); i++)
unsigned int i = 0;
while ( i < sizeof(cors2)/sizeof(cors2[0]))
{
fprintf(results, "%s %d\n", cors2[i].PName, cors2[i].AfterTAB);
i++;
}
printf("\n");
//for ( /*unsigned*/ int g = 0; g < holdingAccount /*sizeof(myrec2)/sizeof(myrec2[0]) */; g++)
//printf("g= %d : Name %s %s amount %d TST %s\n",g+1, myrec2[g].PNameF, myrec2[g].PNameT,
//myrec2[g].Amount, myrec2[g].TST);
fclose(fp);
fclose(results);
return 0;
}