Thread: Transaction Database structure and FILE

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by R0tleSS View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <inttypes.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;
        unsigned __int64 TST;
    };
    struct NOT rec[100];
    
    
    typedef struct NOA{
        char PName[100];
        int ActualAB;
    };
    struct NOA roc[100];
    
    
    typedef struct NOA1{
        char PName[100];
        int AfterTAB;
    };
    struct NOA1 cor[100];
    
    
    void file()
    {
        int i = 1;
        int x = 0;
        int y = 0;
    
    
        FILE *pFile;
        pFile = fopen("D:\\level1-1.txt", "r+");
    
    
        if (pFile != NULL )
        {
            fscanf(pFile, "%d", &x);
         while(fscanf(pFile, "%s\t%d\n", &roc[i].PName, &roc[i].ActualAB) != EOF)
        {
            printf("%s  %d\n",roc[i].PName, roc[i].ActualAB);
            i++;
            if(i>x){
                break;
            }
        }
            fscanf(pFile, "%d", &y);
        while(fscanf(pFile, "%s\t%s\t%d\t%"PRId64"\n", &rec[i].PNameF, &rec[i].PNameT, &rec[i].Amount, &rec[i].TST) != EOF)
        {
            printf("%s  %s  %d  %"PRId64"\n",rec[i].PNameF, rec[i].PNameT, rec[i].Amount, rec[i].TST);
            i++;
        }
            fclose(pFile);
        }
        else
        {
            fprintf(stderr, "\nError opening .txt\n\n");
        }
    }
    
    
    int main(int argc, const char *argv)
    {
    
    
        file();
        return 0;
    }

    Now i am thinking about how to do transferrs... but i don'y have idea how to do... but it is my code that open the file text from the bigining of topic

    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "term2" "term2.c" (in directory: /home/userx/bin)
    term2.c:22:1: warning: useless storage class specifier in empty declaration
     };
     ^
    term2.c:29:1: warning: useless storage class specifier in empty declaration
     };
     ^
    term2.c:36:1: warning: useless storage class specifier in empty declaration
     };
     ^
    term2.c: In function 'file':
    term2.c:54:26: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
          while(fscanf(pFile, "%s\t%d\n", &roc[i].PName, &roc[i].ActualAB) != EOF)
                              ^
    term2.c:63:25: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
         while(fscanf(pFile, "%s\t%s\t%d\t%"PRId64"\n", &rec[i].PNameF, &rec[i].PNameT, &rec[i].Amount, &rec[i].TST) != EOF)
                             ^
    term2.c:63:25: warning: format '%s' expects argument of type 'char *', but argument 4 has type 'char (*)[100]' [-Wformat=]
    term2.c:63:25: warning: format '%ld' expects argument of type 'long int *', but argument 6 has type 'int *' [-Wformat=]
    term2.c:65:16: warning: format '%ld' expects argument of type 'long int', but argument 5 has type 'int' [-Wformat=]
             printf("%s  %s  %d  %"PRId64"\n",rec[i].PNameF, rec[i].PNameT, rec[i].Amount, rec[i].TST);
                    ^
    term2.c: At top level:
    term2.c:77:5: warning: second argument of 'main' should be 'char **' [-Wmain]
     int main(int argc, const char *argv)
         ^
    term2.c: In function 'main':
    term2.c:77:14: warning: unused parameter 'argc' [-Wunused-parameter]
     int main(int argc, const char *argv)
                  ^
    term2.c:77:32: warning: unused parameter 'argv' [-Wunused-parameter]
     int main(int argc, const char *argv)
                                    ^
    Compilation finished successfully.
    I'd say you have some issues, fscanf does not from what I read retrun EOF, it returns what is read, so,
    Code:
    while ( fscanf( "%d%d%d", 1,2,3) == 3 )
    your stubborn need to use argc and argv when your hard coding your file name, why?

    your typedef have issues.

    have you not ran this and seen if it is getting data out of your file?

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    nevermind
    Last edited by userxbw; 12-04-2017 at 04:05 PM.

  3. #18
    Registered User
    Join Date
    Dec 2017
    Posts
    16
    Quote Originally Posted by userxbw View Post
    this is what I figured out, taking the 10 in twos, the first one gives to the second one, the time stamp I just used my system date/ time. I left the giving party with a dollar. all done in one loop. and one loop to print out. I changed TST
    Code:
      
       // int TST;
       char TST[100];
    for system date time.
    Code:
    Gabrielle Terry 345 Mon Dec  4 15:42:55 2017
    
    Foreman Phillips 218 Mon Dec  4 15:42:55 2017
    
    Caitlin Shaffer 117 Mon Dec  4 15:42:55 2017
    
    Trujillo Holman 34 Mon Dec  4 15:42:55 2017
    
    Lynne Kasey 418 Mon Dec  4 15:42:55 2017
    
    // new balances
    
    Gabrielle 1
    Terry 823
    Foreman 1
    Phillips 369
    Caitlin 1
    Shaffer 549
    Trujillo 1
    Holman 369
    Lynne 1
    Kasey 595
    
    
    // old balances
    
    Gabrielle 346
    Terry 478
    Foreman 219
    Phillips 151
    Caitlin 118
    Shaffer 432
    Trujillo 35
    Holman 335
    Lynne 419
    Kasey 177


    ...and how you connect the PName with PNameF and PNameT... for to do transacttion

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by R0tleSS View Post
    ...and how you connect the PName with PNameF and PNameT... for to do transacttion
    you're not done making that code you have work properly
    Code:
    int main(int argc, const char *argv)
    {
     
     
        file();
        
        
        for ( int a = 0; a < 10; a++ )
            printf("In main: %s %d\n", roc[a].PName, roc[a].ActualAB);
            
        printf("\nnext \n");
            
        for ( int d = 0; d < 41; d++)
            printf("%s %s %d %d\n", rec[d].PNameF, rec[d].PNameT, rec[d].Amount, rec[d].TST);
            
        return 0;
    }
    
    
    // gets 
    $ ./term2
    In main:  0
    In main: Gabrielle 346
    In main: Terry 478
    In main: Foreman 219
    In main: Phillips 151
    In main: Caitlin 118
    In main: Shaffer 432
    In main: Trujillo 35
    In main: Holman 335
    In main: Lynne 419
    
    next 
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
      0 0
    Kasey Foreman 35 1506997759
    Terry Trujillo 34 1507001222
    Caitlin Phillips 32 1507014580
    Kasey Shaffer 40 1507023896
    Caitlin Trujillo 48 1507030155
    Shaffer Phillips 35 1507043379
    Phillips Kasey 36 1507056675
    Trujillo Gabrielle 17 1507068897
    Kasey Foreman 48 1507105996
    Shaffer Trujillo 38 1507126199
    Kasey Shaffer 32 1507146419
    Terry Shaffer 41 1507149164
    Kasey Shaffer 21 1507149312
    Phillips Terry 42 1507162028
    Terry Caitlin 46 1507164818
    Lynne Caitlin 49 1507168891
    Shaffer Phillips 46 1507172664
    Lynne Terry 21 1507174181
    Holman Kasey 39 1507177673
    Holman Lynne 26 1507183327
    Terry Caitlin 23 1507193305
    Phillips Holman 45 1507195987
    Terry Lynne 37 1507213247
    Terry Trujillo 29 1507221315
    Terry Gabrielle 44 1507256152
    Holman Kasey 48 1507256853
    Foreman Gabrielle 21 1507258053
    Foreman Lynne 35 1507268302
    Gabrielle Trujillo 43 1507279786
    Caitlin Lynne 31 1507294438
    if you check your file against your output you'll see you're missing records. Two their is a much better way for doing this, your loops are not properly written. your last one only stops because it cannot read any further it has nothing to do with checking for EOF. the way your getting your array size is ball parking (guessing) not needed you have your file for that. for starters.

    you need learn to check your work every step of the way.
    Last edited by userxbw; 12-04-2017 at 04:20 PM.

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this is a little hint, because I know most people don't do it this way, but it works. Instead for declaring your array some astronomical amount, and no using malloc.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include <conio.h>
    #include <string.h>
    
    #include <time.h>
    
    typedef struct NOT {
        char PNameF[100];
        char PNameT[100];
        int Amount;
       // int TST;
       char TST[100];
    }rec;
    typedef struct NOA{
        char *PName; //[100]; one each so i can do this both ways. 
        int ActulAB;
    }roc;
    typedef struct NOA1{
        char PName[100]; // this is in declared a size ahead of time. 
        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;
         
            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;
    
    //get amount of records about to be read into struct.         
    
            fgets(buff, sizeof buff, fp);
            sscanf(buff, "%d", &count);
            
            roc rocs[count]; // make the array the size indicated in file.
            cor cors[count];
            recCount = count;
        
    // that works because it does this at run time. your array is not created until
    //after you get the size or amount of data you're going to use. now that
    // you know your size taken from the file, just apply it to your array size
    // needed. so you don't use more memory then needed, or have to deal
    // with over run when you go back through it. 
    
    
        // USING FGETS AND SSCANF
        // tracker actually kicks it out, check for null is just cuz
      // still fill the buffer for sscanf 
        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++;
        }
    Last edited by userxbw; 12-04-2017 at 06:50 PM.

  6. #21
    Registered User
    Join Date
    Dec 2017
    Posts
    16
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    typedef struct {
    	char	name[20];
    	int		n;
    
    
    } person;
    
    
    int main()
    {
    	FILE 	*fp;
    	FILE	*fo;
    	int		n; /* Number of persons */
    	int		i;
    	int		j;
    	int		nb_t;
    
    
    	fp = fopen("level1-4.txt", "r"); 
    	fo = fopen("result3.txt", "w");  /* and other stuff to call this program etalon of transactions */
    	/* Get number of persons*/
    	fscanf(fp, "%d", &n);
      	person a[n];
      	/* Store data */
      	i = 0;
      	while(i < n) {
     		fscanf(fp, "%s %d", a[i].name, &a[i].n);
     		i++;
     	}
     	/* Get number of transactions */
     	fscanf(fp, "%d", &nb_t);
     	
     	/* Start to make changes (transactions) */
     	j = 0;
     	while(j < nb_t) {
     		char	temp1[20];
     		char	temp2[20];
     		int		am;
     		int		z;
     		long	........;
     		fscanf(fp, "%s %s %d %d", temp1, temp2, &am, &........);
     		i = 0;
     		z = 0;
     		while(i < n) {
     			if(strcmp(a[i].name, temp1) == 0)
     				break;
     			i++;
     		}
     		while(z < n) {
     			if(strcmp(a[z].name, temp2) == 0)
     				break;
     			z++;
     		}
     		a[i].n -= am;
     		a[z].n += am;
     		j++;
    
    
     	}
     	/* Write changes in file */
     	fprintf(fo, "%d\n", n);
     	i = 0;
     	while(i < n) {
     		fprintf(fo, "%s %d\n", a[i].name, a[i].n);
     		i++;
     	}
     	fclose(fo);
     	fclose(fp);
    	return 0;
    }

    What about that program???

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this is bugging you out isn't it? ... you're original set up was fine. It was sweet, it was just a matter of making that work. using that file the way it is formatted using the tools available to do so. Then just using your strut array's to move and changes things then store them. when is this due?

    I' will run this. hold on.


    is that all you're suppose to have?
    Code:
    $ cat result3.txt
    10
    Gabrielle 431
    Terry 200
    Foreman 286
    Phillips 177
    Caitlin 103
    Shaffer 447
    Trujillo 192
    Holman 314
    Lynne 503
    Kasey 57
    /// input file
    $ cat level1-4.txt
    10
    Gabrielle 346
    Terry 478
    Foreman 219
    Phillips 151
    Caitlin 118
    Shaffer 432
    Trujillo 35
    Holman 335
    Lynne 419
    Kasey 177
    41
    Kasey Foreman 35 1506997759
    .....
    looks like it works to me. where did you get the information to deduct an amount? OK I think I see what is going on here, you needed to find them in the file matching between the two in the 10 then transfer the amount shown.
    so who gave what to who? I didn't get this memo. but yeah. OK


    Code:
    10
    Gabrielle 346
    Terry 478
    Foreman 219
    Phillips 151
    Caitlin 118
    Shaffer 432
    Trujillo 35
    Holman 335
    Lynne 419
    Kasey 177
    41
    Kasey Foreman 35 1506997759
    Terry Trujillo 34 1507001222
    Caitlin Phillips 32 1507014580
    Kasey Shaffer 40 1507023896
    Caitlin Trujillo 48 1507030155
    Shaffer Phillips 35 1507043379
    Phillips Kasey 36 1507056675
    Trujillo Gabrielle 17 1507068897
    Kasey Foreman 48 1507105996
    Shaffer Trujillo 38 1507126199
    Kasey Shaffer 32 1507146419
    Terry Shaffer 41 1507149164
    Kasey Shaffer 21 1507149312
    Phillips Terry 42 1507162028
    Terry Caitlin 46 1507164818
    Lynne Caitlin 49 1507168891
    Shaffer Phillips 46 1507172664
    Lynne Terry 21 1507174181
    Holman Kasey 39 1507177673
    Holman Lynne 26 1507183327
    Terry Caitlin 23 1507193305
    Phillips Holman 45 1507195987
    Terry Lynne 37 1507213247
    Terry Trujillo 29 1507221315
    Terry Gabrielle 44 1507256152
    Holman Kasey 48 1507256853
    Foreman Gabrielle 21 1507258053
    Foreman Lynne 35 1507268302
    Gabrielle Trujillo 43 1507279786
    Caitlin Lynne 31 1507294438
    Terry Holman 19 1507333766
    Kasey Holman 28 1507336569
    Kasey Gabrielle 28 1507341386
    Kasey Phillips 11 1507342386
    Trujillo Terry 18 1507359302
    Caitlin Lynne 22 1507359697
    Gabrielle Phillips 14 1507370950
    Gabrielle Phillips 11 1507371099
    Terry Foreman 40 1507372199
    Lynne Gabrielle 43 1507389397
    Terry Lynne 46 1507393424
    two trans actions took place between Kasey --> Foreman 35 + 48 = 83
    Foreman 219 + 83 = 302
    Kasey
    177 - 83 = 94


    is that a correct assessment of results the teacher is looking for?

    but wait,if you look at the ten in the 41 transactions, foreman gave Gabrielle money too, and some others, so that is a lot of book keeping to figure this out if it is completely correct or not. someone that actually knows how to finagle books might know if this will work out in the end. but keeping a cash drawer. anyways,

    I'd try running the one name out of the ten across the entire 41, and where ever that name shows up depending on the position of that person,1st or 2nd then do the math. then do the same to the rest and see what the results are after all of the math is done. first person gives to second person. So you'd have you first array with the ten names, then you'd have to find that missing person in that array to adjust the amount in their holdings. If they go negative anytime during this, the end results should ( maybe) balance out. Like counting out a cash drawer that you start with x amount of money and the receipts to tell you what you should have in your drawer at the end of the day, and you know how to make change, they give you something else then what they started out with or change an item at the same price and you just do it without doing that return it first and then right it up again, the count out will balance it out. well or experience that part of life to understand that mount full.

    even though it kind of looks like that is what you're already doing, I do not have the teachers cheat sheet to see the final results.

    Now that I got a grasp on this

    the first person is giving and the second is receiving YES??? their has to be a pattern here.

    I'm using the code I worked out because it gives me two arrays of ten and the 41 all ready stored for looping and such. that is NOT to say that this code you posted in post 21 is not ingenious.
    Last edited by userxbw; 12-05-2017 at 05:08 PM.

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    getting only part way through this, looking at it again, if you look at your file. Take that first name, you have to use that to find every one it matches with. Using just the first person on the far left and using the first name in the list of 10. how many times do you see it matching up with the list of 41?

    I found 3, so you have to use that name and do three transactions on it. go to the next in the list terry. he gets 7 transactions.

    Code:
    cat R0tleSS_test_file
    10
    Gabrielle 346
    Terry 478
    Foreman 219
    Phillips 151
    Caitlin 118
    Shaffer 432
    Trujillo 35
    Holman 335
    Lynne 419
    Kasey 177
    41
    Kasey Foreman 35 1506997759
    Terry Trujillo 34 1507001222
    Caitlin Phillips 32 1507014580
    Kasey Shaffer 40 1507023896
    Caitlin Trujillo 48 1507030155
    Shaffer Phillips 35 1507043379
    Phillips Kasey 36 1507056675
    Trujillo Gabrielle 17 1507068897
    Kasey Foreman 48 1507105996
    Shaffer Trujillo 38 1507126199
    Kasey Shaffer 32 1507146419
    Terry Shaffer 41 1507149164
    Kasey Shaffer 21 1507149312
    Phillips Terry 42 1507162028
    Terry Caitlin 46 1507164818
    Lynne Caitlin 49 1507168891
    Shaffer Phillips 46 1507172664
    Lynne Terry 21 1507174181
    Holman Kasey 39 1507177673
    Holman Lynne 26 1507183327
    Terry Caitlin 23 1507193305
    Phillips Holman 45 1507195987
    Terry Lynne 37 1507213247
    Terry Trujillo 29 1507221315
    Terry Gabrielle 44 1507256152
    Holman Kasey 48 1507256853
    Foreman Gabrielle 21 1507258053
    Foreman Lynne 35 1507268302
    Gabrielle Trujillo 43 1507279786
    Caitlin Lynne 31 1507294438
    Terry Holman 19 1507333766
    Kasey Holman 28 1507336569
    Kasey Gabrielle 28 1507341386
    Kasey Phillips 11 1507342386
    Trujillo Terry 18 1507359302
    Caitlin Lynne 22 1507359697
    Gabrielle Phillips 14 1507370950
    Gabrielle Phillips 11 1507371099
    Terry Foreman 40 1507372199
    Lynne Gabrielle 43 1507389397
    Terry Lynne 46 1507393424
    Now looking at your matching in your new code.
    Code:
        while(i < n) {
                if(strcmp(a[i].name, temp1) == 0)
                printf("name %s matched with temp1 %s\n",a[i].name, temp1 );
                    break;
                i++;
            }
            while(z < n) {
                if(strcmp(a[z].name, temp2) == 0)
                printf("name %s matched with temp2 %s\n",a[z].name, temp2 );
                    break;
                z++;
            }
    //// here
    $ ./R0tleSS_temp
    name Gabrielle matched with temp2 Gabrielle
    name Gabrielle matched with temp2 Gabrielle
    name Gabrielle matched with temp2 Gabrielle
    name Gabrielle matched with temp1 Gabrielle
    name Gabrielle matched with temp2 Gabrielle
    name Gabrielle matched with temp1 Gabrielle
    name Gabrielle matched with temp1 Gabrielle
    name Gabrielle matched with temp2 Gabrielle
    yeah its a pain in the but. I almost had it was getting seg fault then looked at the file of 41 again to get a perspective on it again. Then noticed my three loops was not even written write, might need to split up in to functions. I myself will try to deal with this tomorrow if I have more time.

    I am using the structs arrays in your first code posted. it makes it easier to reset them and go back through it again.

    needs to basically take one name at a time and search the 41 and match each instance then get the second name that would be in the NOT array along with that name because they are being held in pairs.
    Kasey Foreman 35 1506997759 is in one of the elements, so when you get to Kasey in the NOA struct array then you need to keep that held and match it with every occurrence in the NOA and array that is held ( my logic) in the r PNameF member that gives you the person they are to make a transfer to. the math should works its way out in the end. I made the code to copy the list of 10 into both NOA and NOA1 then I was doing the math on the NOA1 so I have a starting balance to check against the ending balances to see what changes that have taken place.

    Code:
    typedef struct NOT {
        char PNameF[100]; // first persons name
        char PNameT[100]; // second persons name
        int Amount; // amount needed to be transferred. 
       // 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;
    so yeah a lot to do????

  9. #24
    Registered User
    Join Date
    Dec 2017
    Posts
    16
    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;
    }



    The program that you sent me on gmail.. maybe have a bug... my compiler didn't stop
    Last edited by R0tleSS; 12-06-2017 at 10:51 AM.

  10. #25
    Banned
    Join Date
    Aug 2017
    Posts
    861
    that last 3 whiles loop is bad like I said. Strip it out and redo that entire section. but you might want to take a really really good look at it before you do, to see what I WAS TRYING TO DO WITH IT. The logic behind it.

    And you might want to look closely at file names and how they are being opened too. They are not hard coded into the program.

    the rest should be fine you just need to decided which method you're going to keep fgets and sscanf or fscanf to fill your arrays. then write your loops ,2, to search the entire 41 records, while you're figuring out how to do this.
    see if you comment out the entire set of loops it works
    Code:
    userx@slackwhere:~/bin
    $ ./term2 R0tleSS_test_file
    
    
    
     USING FSCANF next 
    
    
    count 10
    Writing to: updating 10 files
    that is what is left of the printfs in there that are not commented out.

    this to me seems the most logical method, even though their maybe other ways of doing this.

    First List.
    Code:
    Gabrielle 346
    Terry 478
    Foreman 219
    Phillips 151
    Caitlin 118
    Shaffer 432
    Trujillo 35
    Holman 335
    Lynne 419
    Kasey 177
    all of them people show up in that 41 list more than once,

    so I used that list keeping that as the first person, Gabrielle hold on to it, while searching the entire 41 records.

    then whence a match is found, then I go into another loop and find that second person to the right of the first persons name when that match is found, then record the transaction. Because your NOT array has both names and the amount transferred.

    then I use that information and the NOA1 copy to record the new balances, updating both parties account balances accordingly, then still keeping Gabrielle I move down the rest of the 41 list to see if he is still there, if match, then goes into second ( inner ) loop repeats process to find second person ... etc.

    After Gabrielle has finished being searched through the entire 41 records, move down one taking Terry, and repeating the entire process. Until you've reached the end of your first list. Then if luck and logic is on your side. you'll ( should )be done.

    ( do try to write it for unknown amounts. using your variables to tell the program how much and not hard coded numbers. If you're not already doing it like that. of course. )

    post back what you've did if you have any problems, and what they are.

    Code:
    Gabrielle Trujillo 43 1507279786
    Caitlin Lynne 31 1507294438
    Terry Holman 19 1507333766
    Kasey Holman 28 1507336569
    Kasey Gabrielle 28 1507341386
    Kasey Phillips 11 1507342386
    Trujillo Terry 18 1507359302
    Caitlin Lynne 22 1507359697
    Gabrielle Phillips 14 1507370950
    Gabrielle Phillips 11 1507371099
    my finished product
    Code:
    Beginning Balance
    
        Name     Amount     Total Transactions 
       Gabrielle    346        3
           Terry    478        10
         Foreman    219        2
        Phillips    151        3
         Caitlin    118        4
         Shaffer    432        3
        Trujillo    35        2
          Holman    335        3
           Lynne    419        3
           Kasey    177        8
    
    Updated Account
    
        Name    Amount in account Total Transactions
       Gabrielle    431            3
           Terry    200            10
         Foreman    286            2
        Phillips    177            3
         Caitlin    103            4
         Shaffer    447            3
        Trujillo    192            2
          Holman    314            3
           Lynne    503            3
           Kasey    57            8
    Transactions Processed 41
    Last edited by userxbw; 12-06-2017 at 11:32 AM.

  11. #26
    Registered User
    Join Date
    Dec 2017
    Posts
    16
    You check firstly the number of transaction for everybody in part????

  12. #27
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by R0tleSS View Post
    You check firstly the number of transaction for everybody in part????
    I did it just like i wrote it out for you, with the number of transactions is being added to each person when they have one, I added a member to the struct's to keep count of transactions.
    the new structs for number of transactions
    Code:
    typedef struct NOT {
        char PNameF[100];
        char PNameT[100];
        int Amount;
       // int TST; 
       char TST[100]; //for timestamp
    }rec;
    typedef struct NOA{
        char *PName; //[100]; requires strcpy for ptr.
        int ActulAB;
        int totalTransactions;
    }roc;
    typedef struct NOA1{
        char PName[100];
        int AfterTAB;
        int totalTransactions;
    }cor;
    Code:
    cors2[firstPerson].totalTransactions += 1;
    rocs2[firstPerson].totalTransactions += 1;
    look at your post of my code, and look at the while loops there is a screw up in there maybe more than one to keep count of things being done but look at how I am using them to keep one and search for another ..

    study that logic a little more. fiddle with them loops look at its output, then rewrite it to make it work. that is what I did, even though I striped it all out of there then redid the entire thing, I used the same basic logic to get it to work.

    you need to read what I said and pay attention to it I gave you clues the best I could.
    Last edited by userxbw; 12-06-2017 at 11:42 AM.

  13. #28
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Let me try to put it in simplier terms.

    you have 2 list. first list is x amount long. it contains the single name and amount in their account.
    second list has two names, they both are in the first list. it contains who they are, and the amount taken from the first person and given to the second person.
    the second list is longer then the fist list. The people in the first list are listed more then once as first person.

    bob sally 43 3453335
    sally jill 46 3343434
    bob sally 34 3432424355

    the puzzle is how to find all of the occurrences of the first person in the second list then find who they gave their money to, then make that show in the amounts they have in both of their accounts. because one is going to now show a loss and the other is going to show a gain respectively to the same amount given in the second list.

    so bob has to be held onto until that entire second listings is looked through to make sure that all of bobs transaction took place between each second person and the same amount is used in the second list.

    the fist list is shorter then the second list. this requires some special thinking. because you have to have a means to control the loops. it is like searching through a directory and its sub directories before moving on to the next parent directory.

    you have to keep hold of the parent directory until you've looked through the entirety of its sub directories before moving on the the next parent directory.

    in other words it requires two loops , one to hold the parent and the second to look into the sub directories. when it is done move down to the next parent directory.

    the thing is your "parent directory" has less then the "sub directories". First list, Second list. so you cannot use the second list for a means to control your loops. because you're using the smaller list as a means of control over the longer list.

    basically it is just searching two arrays looking for matches between the two when one array is larger then the other and has a same match more than once in that array.

    it is just the matter of figuring out when to shift the first array that you're using to check against the larger array. after its looked through it completely so it can then use the next element and do the same thing to it.

    Code:
    int array1 [ ] = { 1, 3 , 5, 6};
    int array2 [ ] = { 3, 1, 5, 6, 3, 1, 5};
    write a program that finds the amount of occurrences for each element value in the second array with the first array. In other words how many times does 1 , 3, 5, 6 show up in the second array. write a program that will show the amount of times this occurs using both arrays.
    Code:
    1 shows up x amount of times
    3 shows up x amount of times
    5 shows up x amount of times
    6 shows up x amount of times
    where x amount of times is the actual amount. using while loops

    then build on that knowledge. remembering you're using a pseudo 3 array to look into. the first "two" hold both names of the "third array" that contains the information of the two parties and amount transferred.

    you have to use that one array twice in the operation of this. before moving on the the next person in the first list (array)
    once for the fist person in the "third" NOT array , and again for the second person in the NOT struc array. yes it can sound confusing.

    Code:
    int array1 [ ] = { 1, 3 , 5, 6};
    int array2 [ ] = { 3, 1, 5, 6, 3, 1, 5};
    write a program that finds the amount of occurrences for each element value in the second array with the first array. In other words how many times does 1 , 3, 5, 6 show up in the second array. write a program that will show the amount of times this occurs using both arrays.
    Code:
    1 shows up x amount of times
    3 shows up x amount of times
    5 shows up x amount of times
    6 shows up x amount of times
    Now match them up in pairs. 1 and 5 and 3 and 6. when you find both make a note of it using an int to keep count of how many times they matched. something like that.

    probably over explained myself.
    Last edited by userxbw; 12-06-2017 at 12:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database structure
    By eterna in forum C Programming
    Replies: 28
    Last Post: 03-31-2013, 04:23 PM
  2. structure database..?
    By hegdeshashi in forum C Programming
    Replies: 1
    Last Post: 07-25-2006, 09:27 AM
  3. (structure+array+pointer)to make a simple database
    By frankie in forum C Programming
    Replies: 5
    Last Post: 04-26-2002, 05:14 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

Tags for this Thread