• C

# yet Another simple log totaler

I would like my ap to open the log file below and read all of the dollar amounts, then show the total dollar amount. I want to use C++ for this....

01/01 15:36 to 01/01 15:56 User Min = 67 Cost = \$0.17
01/01 15:56 to 01/01 16:13 User Min = 264 Cost = \$0.66
01/01 16:13 to 01/01 17:18 User Min = 1614 Cost = \$4.04
01/01 17:18 to 01/01 17:18 User Min = 0 Cost = \$0.00
01/01 17:18 to 01/01 18:03 User Min = 1285 Cost = \$3.21
01/01 18:03 to 01/01 19:15 User Min = 1827 Cost = \$4.57
01/01 19:15 to 01/01 20:28 User Min = 1723 Cost = \$4.31
01/02 15:39 to 01/02 20:28 User Min = 8683 Cost = \$21.71
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Edited text of question.
0
Commented:
first comment "I want to use C++ for this.... ". So, why didn't you post in the C++ area ?

Use strtok().  I don't guarantee that the following will compile, but it's near enough what you want. Let me know if you want me to write & test  the code.

while (!eof(file))
{
char *p;
char *line;

gets(line);

p = strtok(line, "\$");
p = strtok(NULL, "\$");

total += atoi(p);
}
0
Commented:
your profile seems to show that you are gainfully employed. I sincerely hope that this is not homework.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv)
{
FILE *input;
char  line[255];    /* you might want to change this, or make it a #define */
float amount;
float total = 0;
char  in_file[] = "logfile1.txt";
char *p;

/* you can also pass the log file as a parameter. Simply in_file = argv[1]; */
input = fopen(in_file, "rt");
if (input == NULL)
{
fprintf(stderr, "Can't open input file [%s]!!\n", in_file);
exit(0);
}

while (!feof(input))
{
fgets(line, sizeof(line), input);
p = strtok(line, "\$");
p = strtok(NULL, "\$");
p[strlen(p) - 1] = '\0';    /* delete carriage return */

sscanf(p,"%f", &amount);    /* get it as a float (with decimal) */
printf("\$%#4.2f \n\n", amount);
total += amount;
}
printf("\nGrand total = \$%#6.2f \n", total);
}
0

Experts Exchange Solution brought to you by