I'm having a problem with data files.

This is a discussion on I'm having a problem with data files. within the C Programming forums, part of the General Programming Boards category; I figured out how to get my pointers and such to work while the program results were printed on the ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    I'm having a problem with data files.

    I figured out how to get my pointers and such to work while the program results were printed on the screen. However, when I try to switch over to datafiles (with an input and output file), my input file goes in (I think), and nothing comes out on my output file except for the "Total amount:" footer. No calculations were done at all.

    I checked and my fscan and fprints were stated correctly, as far as I know (the other print statments are going on the compile screen, not the output file). I just can't figure out what I am missing. Here is my code.

    Code:
    #define ZERO 0.0
    #define COMMISSION_RATE 0.144
    #define BONUS_COMM_RATE 0.188
    #define TRUE 1
    #define FALSE 0
    #define HIGH_SALESPERSON 7
    #define ARRAY_SIZ 75
    #define READ "r"
    #define INPUT "inputfile7.in"
    #define WRITE "w"
    #define OUTPUT "outputfile7.out"
    #include <stdio.h>
    
    int input_fun(int *s_code, int *c_code, double *sale_amount, int 
    
    count,             char *spv_str, char *cust_str, char *sale_str, char 
    
    *sale_err);
    
    void output_fun(int *s_code, int *c_code, double 
    
    *sale_amount,double *sale_comm,  int *flag, int number, double 
    
    saletotal, double commtotal,char *report_header, char 
    
    *column_header, char *bonus_mark, char *no_mark, char 
    
    *all_total,          char *sale_count);
    
    int sales_code(FILE *in, char *spn_str);
    int cust_code(FILE *in, char *cust_str);
    double sale(FILE *in, char *sale_str);
    void sale_error(char *sale_err);
    main()
    {
       int s_code[ARRAY_SIZ];
       int c_code[ARRAY_SIZ];
       double sale_amount[ARRAY_SIZ];
       double sale_comm[ARRAY_SIZ];
       int flag[ARRAY_SIZ];
       char *spn_str = "Salesperson code (1-7)(Enter 0 to quit): ";
       char *cust_str = "Customer code: ";
       char *sale_str = "Sale amount in dollars and cents: ";
       char *sale_err = "Salesperson code must be 1 to 7";
       char *report_header = "        SALES REPORT";
       char *column_header = "SP CUST      SALE         COMM";
       char *bonus_mark = "**";
       char *no_mark = "  ";
       char *all_total = "Total:  ";
       char *sale_count = "Number of sales: ";
    
       int num_sales = 0;
       double total_sales, total_comm = 0;
       int n = 0;
          num_sales = input_fun(s_code, c_code, sale_amount, num_sales, spn_str,           
     cust_str, sale_str, sale_err);
          for (n = 0; n < num_sales; n++)
          {
             if (sale_amount[n] > 1450)
             {
                flag[n] = TRUE;
               sale_comm[n] = (208.8 + ((sale_amount[n] - 1450) * BONUS_COMM_RATE));
             }
             else
             {
                flag[n] = FALSE;
                sale_comm[n] = (sale_amount[n] * COMMISSION_RATE);
             }
             total_sales += sale_amount[n];
             total_comm += sale_comm[n];
          }
             output_fun(s_code, c_code, sale_amount, sale_comm, flag, num_sales,            
     total_sales, total_comm, report_header, column_header, 
    
    bonus_mark,              no_mark, all_total, sale_count);
    }
    
    
    int
    input_fun(int *s_code, int *c_code, double *sale_amount, 
    int 
    count,               char *spn_str, char *cust_str, char *sale_str, 
    
    char *sale_err)
    {
       FILE *in;
       if (( in = fopen(INPUT, READ)) == NULL)
       {
          printf("\nCannot open file\n");
          exit(1);
       }
       s_code[count] = sales_code(in, spn_str);
       while (!EOF)
       {
          if (s_code[count] <= HIGH_SALESPERSON && s_code[count] > ZERO)
          {
             c_code[count] = cust_code(in, cust_str);
             sale_amount[count] = sale(in, sale_str);
             count++;
             s_code[count] = sales_code(in, spn_str);
          }
          else
          {
             sale_error(sale_err);
             s_code[count] = sales_code(in, spn_str);
          }
       }
       fclose(in);
       return count;
    }
    
    void
    output_fun(int *s_code, int *c_code, double *sale_amount, 
    
    double *sale_comm,    int *flag, int number, double saletotal, 
    
    double commtotal, char *report_header,  char *column_header, 
    
    char *bonus_mark, char *no_mark, char *all_total,          char *sale_count)
    {
       int o = 0;
       FILE *out;
       out = fopen(OUTPUT, WRITE);
       for (o = 0; o < number; o++)
       {
          fprintf(out, "\n%d  %3d  $%9.2lf", s_code[o], c_code[o], sale_amount[o]);
          if (flag[o] == TRUE)
          {
             fprintf(out, "%s", bonus_mark);
          }
          else
          {
             fprintf(out, "%s", no_mark);
          }
          fprintf(out, "  $%9.2lf", sale_comm[o]);
       }
          fprintf(out, "\n\n%s", all_total);
          fprintf(out, "$%9.2lf    $%9.2lf", saletotal, commtotal);
          fprintf(out, "\n%s", sale_count);
          fprintf(out, "%d", number);
          fclose(out);
       return;
    }
    
    
    
    int
    sales_code(FILE *in, char *spn_str)
    {
       int sval;
          fscanf(in, "%d", &sval);
          return sval;
    }
    
    int
    cust_code(FILE *in, char *cust_str)
    {
       int cval;
       fscanf(in, "%d", &cval);
          return cval;
    }
    
    double
    sale(FILE *in, char *sale_str)
    {
       double amount;
       fscanf(in, "%lf", &amount);
          return amount;
    }
    
    
    void
    sale_error(char *sale_err)
    {
       printf("%s", sale_err);
          return;
    }
    The file is constructed like this

    (saleperson number, customer number, sale amount)

    xxx xx xxx.xx
    xxx xx xxx.xx

    and so on. While I know my other code is wrong for now, I feel that I have written the open and close file statements incorrectly. If they were correct, I would at least get some numbers before the program was thrown out of whack.

    Also, I'm getting a "statement not reached" warning for this line

    Code:
    if (s_code[count] <= HIGH_SALESPERSON && s_code[count] > ZERO)
    Is there a different way to write my file statements or is this actually the correct way? I'm pretty unsure at the moment.

    Thanks in advance.

    (The browser was really awful, so some of my code might be split up in a funky way (in particular on the function headers))
    Last edited by OmniMirror; 05-14-2003 at 05:20 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while (!EOF)
    That's your problem. Your loop never executes. !EOF should evaluate to zero, which is false.

    See:

    printf("%d", !EOF );

    Well, that's one of your problems anyway.

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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    And before you go changing it to something you shouldn't, you might want to read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Thanks guys. Especially for that section of the FAQ. The while(!EOF) did seem logical at the time...

    Anyway, time to get crackin' again. Thanks!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    The FAQ worked like a charm. Thanks! Everything works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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