• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
0
vernk
Asked:
vernk
  • 3
  • 2
1 Solution
 
vernkAuthor Commented:
Edited text of question.
0
 
graham_kCommented:
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
 
graham_kCommented:
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
 
vernkAuthor Commented:
Thank you! Sorry about the wrong forum, I realized i had clicked on the C forum after posting.....
0
 
graham_kCommented:
hmm, well, I gave you straight C. It does what you want, but it's not C++.  I'll leave that as "an exercise for the reader" :-)
0

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now