Help with pointers and structs

This is a discussion on Help with pointers and structs within the C Programming forums, part of the General Programming Boards category; Hey guys-- I am stuck on the very last program of this semester Problems with program: 1. Input file is ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Help with pointers and structs

    Hey guys-- I am stuck on the very last program of this semester

    Problems with program:
    1. Input file is only accepting the name, no other fields.
    2. Outputting only the headers, no info from the data file.

    I know the problem is with the struct, I am allocating memory for a number of records, but I am not sure how to access them one at a time and retrieve them in the proper sequence.

    I am also including what the output should look like.

    The Code:


    Code:
     #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define CLS                     printf("\033[2J")
    #define LOCATE(r,c)             printf("\033[%d;%dH",r,c)
    #define PAUSE LOCATE(23,20);    printf("Press Any Key to Continue");
    getch();
    
    FILE *fileptr;
    FILE *stream;
    float ftotal;
    int i, linecount;
    
    
    
    struct employees
    {char name[20];
    int pnumber;
    int dnumber;
    int snumber;
    float amount;
    }person[5];
    
    void paintscreen(), addrecord(), enterdata();
    void printheaders(), processdetail();
    void printfinaltotal(), printdetailline();
    void printpersontotal(float *ptotal, int *pcontrol, float *dtotal);
    void printdeparttotal(float *dtotal, int *dcontrol);
    void printstoretotal(float *stotal, int *scontrol);
    
    void main()
    {stream=fopen("c:\p7adv.out", "w");
    addrecord();
    if((fileptr=fopen("a:sales", "r"))==NULL)
      {puts("Can't open file");
       PAUSE;
       exit(1);
       }
     printheaders();
     processdetail();
     printfinaltotal();
     fclose(fileptr);
     fclose(stream);
     }
    
    void addrecord()
    {fileptr=fopen("a:sales", "a");
     paintscreen();
     while(strlen(gets(person[i].name)) != 0)
     {enterdata();
      paintscreen();
     }
     fwrite(person, sizeof(person[0]), i, fileptr);
     i=0;
     fclose(fileptr);
     }
    
    void paintscreen()
    {CLS;
    LOCATE(7,35);
    puts("Data Entry Screen");
    LOCATE(10,10);
    puts("Employee Name: ");
    LOCATE(12,10);
    puts("Emp. Number: ");
    LOCATE(14,10);
    puts("Department Number: ");
    LOCATE(16,10);
    puts("Store Number: ");
    LOCATE(18,10);
    puts("Amount of sales: ");
    LOCATE(10,26);
    fflush(stdin);
    }
    
    void enterdata()
    {LOCATE(12,24);
     scanf("%d", person[i].pnumber);
     LOCATE(14,30);
     scanf("%d", person[i].dnumber);
     LOCATE(16,25);
     scanf("%d", person[i].snumber);
     LOCATE(18,27);
     scanf("%f", person[i++].amount);
      }
    
    void printheaders()
    {static int pagecount = 1;
     fprintf(stream, "\t        Sales Report        Page %d\n\n\r", pagecount++);
    fprintf(stream, "\tName   \t   Department     \t  Amount");
     linecount = 3;
     }
    
     void printfinaltotal()
     {fprintf(stream, "Final Total         %5.2f \n\r",  ftotal);
     }
    
     void printdetailline()
     {if(++linecount>50)
      {printheaders();
       linecount++;
       }
      fprintf(stream, "%-20s           %d         %d      %5.2f \n\r", person[i].name, person[i].dnumber, person[i].snumber, person[i].amount);
      }
    
     void processdetail()
     {
     int pcontrol, dcontrol, scontrol;
      float ptotal = 0.0;
      float dtotal = 0.0;
      float stotal = 0.0;
      fread(person, sizeof(person), 1, fileptr);
      pcontrol = person[i].pnumber;
      dcontrol = person[i].dnumber;
      scontrol = person[i].snumber;
      do
       if(person[i].dnumber != dcontrol)
        {
        printpersontotal(&ptotal, &pcontrol, &dtotal);
        printdeparttotal(&dtotal, &dcontrol);
        }
       else
       if(person[i].pnumber != pcontrol)
         {
         printpersontotal(&ptotal, &pcontrol, &dtotal);
         printdetailline();
         ptotal += person[i].amount;
         }
         while (fread(person, sizeof(person), 1, fileptr));
      printpersontotal(&ptotal, &pcontrol, &dtotal);
      printdeparttotal(&dtotal, &dcontrol);
      printstoretotal(&stotal, &scontrol);
      }
    
      void printpersontotal(float *ptotal, int *pcontrol, float *dtotal)
      {fprintf(stream, "\n\r  Person Total   %5.2f \n\n\r", *ptotal);
       *dtotal += *ptotal;
       *ptotal = 0.0;
       *pcontrol = person[i].pnumber;
       linecount += 3;
       }
    
      void printdeparttotal(float *dtotal, int *dcontrol)
       {fprintf(stream, "\n\r  Department Total    %5.2f \n\n\r", *dtotal);
        ftotal += *dtotal;
        *dtotal = 0;
        *dcontrol = person[i].dnumber;
        linecount += 3;
        }
    
      void printstoretotal(float *stotal, int *scontrol)
      {fprintf(stream, "\n\r  Store Total        %6.2f \n\n\r", *stotal);
       ftotal += *stotal;
       *stotal = 0;
       *scontrol = person[i].snumber;
        linecount += 3;
       }
    The output should look like this (I am only getting the headers):

    Code:
      Sales Report				page 1
    Name		Department		store  		Amount
    M. Smith	100			1		10.00
    M.Smith		100			1		20.00
    
    Person Total:	30.00
    
    B. Smith	100			1		40.00
    
    Person total:	40.00
    
    Department Total:  70.00
    
    C. Smith	200			1		50.00
    
    Person Total:  50.00
    
    Department Total:  70.00
    
    Store Total:  120.00
    
    D. Smith	100			2		60.00
    
    Person Total:  60.00
    
    Department Total:  60.00
    
    Store Total:   60.00
    
    Final Total:   180.00

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're using fread and fwrite to write your structures. As such, you should be using "b" for binary mode to open the files.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    Could you explain that to me please?

    Also how do I extract an individual record from that struct?

    Do I have to use a struct pointer to do that?
    Last edited by NewToC; 12-12-2002 at 06:15 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just read up on how fopen works.

    fopen( "myfile" "r" ); /* open for TEXT mode, read only */
    fopen( "myfile", "rb" ); /* open for BINARY mode, read only */

    As for your other question, I'm not clear on what you mean.
    Code:
    struct mystruct
    {
        int x;
        char name[BUFSIZ];
    };
    
    struct mystruct mstrct;
    
    mstrct.x = 10; /* access one member of the structure */
    strcpy( mstrct.name, "Jim Beam" ); /* access on member of the structure */
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    I use a 'learnc' complier and I was never taught the 'rb' mode.

    I am only familiar with the 'r', 'r+', 'w', 'w+', 'a', 'a+' modes.

    I am not getting anything from the infile in output and the infile seems to be only accepting the Name, and not the store, dept. number, etc.

    What is wrong with the code I gave and how can I improve on it?

    The program gets handed in early next week and I have no time to completely redo it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already answered. If you use fwrite and fread, do not use text mode. Use something else like fgets or fputs for reading and writing. Read the link I gave you and it will tell you about binary files.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    quzah,
    the man page says

    This is strictly for compatibility with ANSI C3.159-1989 (``ANSI C'') and has no effect; the ``b'' is ignored.
    Is this only for *NIX systems? Does windows require the b?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by orbitz
    quzah,
    the man page says...


    Is this only for *NIX systems? Does windows require the b?
    Interesting. It used to be required. Apparently that's not the case any more. At one time there was a distinction between the two. MSVC++ 6 doesn't require it to write in binary mode. I suppose I should pay attention to the links I use. I've always used 'b' when I needed binary out of habbit, because at one time it was a requirement.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    Thanks guys,

    Prof is very particular of code, and the code he accepts is very convoluted. Quzah I will use the links you kindly gave for future reference.

    Thanks again you guys were very informative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 01:35 AM
  2. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  3. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 05:29 AM
  4. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 03:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21