manipulating .dat file

Hi everybody,

I read on here a lot but this is my first post. I have to create a program and after days of looking have not found an answer that i am content with.

My program has to take a .dat file which has a "product number", "description", "expense", and "revenue" in the fields must be manipulated so that i can output the net profit for each one. I have the code that I have so far from my book, teacher, and reading online, I can get the data file to create, however I am not sure how to actually find the net profit for each line in the .dat file. Here is my code so far so that everybody knows I have been working on it, I just need to add one more line for the revene, but that is arbitary, the real problem is that i cannot figure out how to calculate the net profit.


I am very new to programming and have struggled greatly, any help would be very much appreciated.



CODE:

***This is the basic idea of where im at right now, i still need to add one more line for the user to add the revenue and then change a couple arbitary names which were used to keep my code, the books, and my teachers all similar.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>

int main( void )
{ 
   int account;    		 /* account number */
   char name[ 30 ]; 		/*  account name */
   double balance;  		/*  account balance */
   int request; /* request number */
   FILE *cfPtr;     /* cfPtr = clients.dat file pointer */

   /* fopen opens file. Exit program if unable to create file  */

   if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
      printf( "File could not be opened\n" );
   } /* end if */
   else { 
      printf( "Enter the account, name, and balance.\n" );
      printf( "Enter EOF to end input.\n" );
      printf( "? " );
      scanf( "%d%s%lf", &account, name, &balance );

      /* write account, name and balance into file with fprintf */
      while ( !feof( stdin ) ) { 
         fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
         printf( "? " );
         scanf( "%d%s%lf", &account, name, &balance );
      } /* end while */
      
      fclose( cfPtr ); 			/* fclose closes file */
   } /* end else */

     // FILE *cfPtr;     		/* cfPtr = clients.dat file pointer */

   /* fopen opens file; exits program if file cannot be opened */ 
//------------------------------------------------------------------------------------------------



   if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
      printf( "File could not be opened\n" );
   } /* end if */
   else { /* read account, name and balance from file */
      printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
      fscanf( cfPtr, "%d%s%lf", &account, name, &balance );

      /* while not end of file */
      while ( !feof( cfPtr ) ) { 
         printf( "%-10d%-13s%7.2f\n", account, name, balance );
         fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
      } /* end while */

      fclose( cfPtr ); 		/* fclose closes the file */
   } /* end else */








   //----------------------------------------------------------------


   /* fopen opens the file; exits program if file cannot be opened */
   if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
      printf( "File could not be opened\n" );
   } /* end if */
   else {
      
      /* display request options */
      printf( "Enter request\n"
         " 1 - List accounts with zero balances\n"
         " 2 - List accounts with credit balances\n"
         " 3 - List accounts with debit balances\n"
         " 4 - End of run\n? " );
      scanf( "%d", &request );

      /* process user's request */
      while ( request != 4 ) { 

         /* read account, name and balance from file */
         fscanf( cfPtr, "%d%s%lf", &account, name, &balance );

         switch ( request ) { 

            case 1:
               printf( "\nAccounts with zero balances:\n" );

               /* read file contents (until eof) */
               while ( !feof( cfPtr ) ) { 

                  if ( balance == 0 ) {
                     printf( "%-10d%-13s%7.2f\n", 
                        account, name, balance );
                  } /* end if */

                  /* read account, name and balance from file */
                  fscanf( cfPtr, "%d%s%lf", 
                     &account, name, &balance );
               } /* end while */

               break;

            case 2:
               printf( "\nAccounts with credit balances:\n" );

               /* read file contents (until eof) */
               while ( !feof( cfPtr ) ) { 

                  if ( balance < 0 ) {
                     printf( "%-10d%-13s%7.2f\n", 
                        account, name, balance );
                  } /* end if */

                  /* read account, name and balance from file */
                  fscanf( cfPtr, "%d%s%lf", 
                     &account, name, &balance );
               } /* end while */

               break;

            case 3:
               printf( "\nAccounts with debit balances:\n" );

               /* read file contents (until eof) */
               while ( !feof( cfPtr ) ) { 

                  if ( balance > 0 ) {
                     printf( "%-10d%-13s%7.2f\n", 
                        account, name, balance );
                  } /* end if */

                  /* read account, name and balance from file */
                  fscanf( cfPtr, "%d%s%lf", 
                     &account, name, &balance );
               } /* end while */

               break;           
            
         } /* end switch */ 

         rewind( cfPtr ); /* return cfPtr to beginning of file */

         printf( "\n? " );
         scanf( "%d", &request );
      } /* end while */

      printf( "End of run.\n" );
      fclose( cfPtr ); /* fclose closes the file */
   } /* end else */

   system("pause");


   return 0; /* indicates successful termination */

} /* end main */




Last edited on
any help would be greatly appreciated, i feel im missing something small in order to get the net profit to work.
According to this:

http://en.wikipedia.org/wiki/Net_profit

you have to add all costs and revenues and subtract them:
Wikipedia wrote:
Net profit ($) = Sales revenue ($) - Total costs ($)


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Hi Coder777,

I am aware the mathematical formula for how to find net profit, however my issue is in the code, can I create a variable "netProfit" and just subtract the two variables revenue and expense, even though they are in a .dat file.

If I can how would the code look like?

Here is the code that I would modify, for example:

With this code my issue is how to create the variable balance which is relevant to each .dat line. I appreciate the help, im sure i am missing something small which is holding me back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
case 1:
               printf( "\nAccounts with zero balances:\n" );

               /* read file contents (until eof) */
               while ( !feof( cfPtr ) ) { 

                  if ( balance == 0 ) {
                     printf( "%-10d%-13s%7.2f\n", 
                        account, name, balance );
                  } /* end if */

                  /* read account, name and balance from file */
                  fscanf( cfPtr, "%d%s%lf", 
                     &account, name, &balance );
               } /* end while */




Also so that everybody is aware, here is the assignment outline:
Create a software solution to the following problem: A simple product application system is needed with the following specifications:
The sequential file (which you have to create) is shown below. Name the data file product.dat It has the following test data:

0001 Irons 200.00 100.00
0002 Curlers 50.00 150.00
0003 Dryers 20.00 60.00
0004 Scissors 900.00 100.00


Product Product
Number Description Revenue Expenses

0001 Irons 200.00 100.00
0002 Curlers 50.00 150.00
0003 Dryers 20.00 60.00
0004 Scissors 900.00 100.00

Write a c program (you also have to create the data file) that will allow the user a number of choices to process and view the information.
The main program should allow the user to enter one of five choices. They are listed below.

Choice 1: Show all of the records in the file, including the Product Number, Product Description, Revenue, Expenses, as well as Net Profit (or Net Loss). The Net Profit (or Net Loss) is calculated by subtracting the Expenses from the Revenue Amount. Additionally show totals (accumulators) for all of the products, including Total Revenue, Total Expenses, and Net Income for each product.

Choice 2: Show all of the records in the file, including the Product Number, Product Description, Revenue, Expenses, as well as Net Profit, in which the Net Profit is greater than 0.00 and less than or equal to 100.00.

Choice 3: Show all of the records in the file, including the Product Number, Product Description, Revenue, Expenses, as well as Net Profit, in which the Net Profit is greater than 500.00 and less than or equal to 1000.00.

Choice 4: Show all of the records in the file, including the Product Number, Product Description, Revenue, Expenses, as well as Net Profit (Net Loss), in which the Net Loss is a less than 0.0.

Choice 5: Show Summary Totals for the Total Revenue, Total Expenses, and Total Net Income for all of the records in the file.

Choice 6: End Program Run

Be sure that your program rewinds the file for each choice made (excluding choice six (ending the program run).


***This is very important:
Right now I am aware that I can only input three variables, this is due to how we practiced it, I wanted to try a simple program before moving on, now all i need to do is add one more user input variable and then calculate net profit, from there the case selection just needs minor modifications
Last edited on
I finally figured it out, for anybody interested:

1
2
3
4
5
6
7
8
9
Net Profit Code
   fscanf(readPtr, "%d%s%f%f", &prodNum, prodDesc, &revenue, &expenses);

          while(!feof(readPtr))
        {
                net = revenue - expenses;
                printf("%-12d%-12s%-12f%-12f%-12f\n", prodNum, prodDesc, revenue, expenses, net);
               fscanf(readPtr, "%d%s%f%f", &prodNum, prodDesc, &revenue, &expenses);
        }



I have one more issue, before I get to the code above in order to find the net profit, there is a error where the file keeps repeating numbers infinitely down, here is the code which is giving me a problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
      printf( "File could not be opened\n" );
   } /* end if */
   else { /* read account, name and balance from file */
      printf( "%-10s%-13s%s%s\n", "Account", "Name", "Balance", "    Revenue" );
      fscanf( cfPtr, "%d%s%lf%1f", &account, name, &balance, &revenue );

      /* while not end of file */
      while ( !feof( cfPtr ) ) { 
         printf( "%-12d%-12s%-12f%-12f%-12\n", account, name, balance, revenue );  /*Modify this line for the output*/
         fscanf( cfPtr, "%d%s%1f%f", &account, name, &balance, &revenue );
      } /* end while */

      fclose( cfPtr ); 		/* fclose closes the file */
   } /* end else */


I believe that the error is in the escape sequence, but I cannot be sure, any help once again would be greatly appreciated!
Last edited on
Topic archived. No new replies allowed.