• C

Simple file reading/totaling routines

I would like commented code to learn some basic file reading concepts. I want to use C++, not C.  I have a log file and I want my program to open the file, read it, and output the following:

name
date
$ amount

name
date
$ amount

Total of $ amounts


example log file below:

------------------------------------------------------------------------
DATE & TIME:  Wed Dec 01 17:34:49 1999
NAME           :  Jim Fritz
ADDRESS        :  1202 Alta Carmel Court
CITY              :  San Diego
STATE         :  CA
ZIP CODE      :  92128-3829
COUNTRY       :  USA
EMAIL ADDRESS :  rfrz1@sa.com
PHONE         :  619-555-5555
IP ADDRESS    :  204.210.54.112
REFERING SITE :  10000 (thesite.com)
SYS ID        :  727319
WEBSITE NAME  :  asite.com
#             :  $9.80
USER FIELD #1 :  800-356-0011
USER FIELD #2 :  N/A
USER FIELD #3 :  N/A
USER FIELD #4 :  N/A
USER FIELD #5 :  N/A
------------------------------------------------------------------------
DATE & TIME   :  Tues Jan 01 12:33:46 2000
NAME          :  john citizen
ADDRESS       :  12 some street
CITY          :  San Diego
STATE         :  CA
ZIP CODE      :  92128-3829
COUNTRY       :  USA
EMAIL ADDRESS :  rrz1@a.com
PHONE         :  619-555-5555
IP ADDRESS    :  63.254.54.112
REFERING SITE :  10001 (thesite.com)
SYS ID        :  727319
WEBSITE NAME  :  te.com
#             :  $9.80
USER FIELD #1 :  800-356-0011
USER FIELD #2 :  N/A
USER FIELD #3 :  N/A
USER FIELD #4 :  N/A
USER FIELD #5 :  N/A

vernkAsked:
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.

vernkAuthor Commented:
Edited text of question.
0
graham_kCommented:
you could pretty up the output layout a bit, but this does what you want ...

#include <stdio.h>

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

  /* 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))
  {
    /* we have two choices. If the logfile is *definitely* fixed format & the */
    /* lines in the same places, we can simply read lines & ignore some.      */
    /* Otherwise, we have to read each line & check the first few chars for a */
    /* keyword, in order to know what kind of a line this is.                 */
    /* The sample log file seems to be fixed format, so we do it the easy way */

    /* note that I have a lot of explicit fgets() calls, just to give         */
    /* comments. Better might be to count them & have one fgets() in a for    */
    /* loop. It would certainly look tidier :-)                               */


    fgets(line, sizeof(line), input);   /* separator  -------- */
    fgets(line, sizeof(line), input);   /* date & time */
    strcpy(date, &line[17]);            /* thats date AND time, do you want date only?*/
    /* note that gets() includes the newline character at the end of the      */
    /* string. I have not removed it, as I use it to format the output.       */
    /* If you want to remove it line[strlen(line) - 1] = '\0';                */
    fgets(line, sizeof(line), input);   /* name */
    strcpy(name, &line[17]);
    fgets(line, sizeof(line), input);   /* address - ignore */
    fgets(line, sizeof(line), input);   /* city - ignore */
    fgets(line, sizeof(line), input);   /* state - ignore */
    fgets(line, sizeof(line), input);   /* zip - ignore */
    fgets(line, sizeof(line), input);   /* country - ignore */
    fgets(line, sizeof(line), input);   /* e-mail - ignore */
    fgets(line, sizeof(line), input);   /* phone - ignore */
    fgets(line, sizeof(line), input);   /* IP address - ignore */
    fgets(line, sizeof(line), input);   /* referer - ignore */
    fgets(line, sizeof(line), input);   /* sys Id - ignore */
    fgets(line, sizeof(line), input);   /* website - ignore */
    fgets(line, sizeof(line), input);   /* amount */
    sscanf(&line[18],"%f", &amount);    /* get it as a float (with decimal) */
    printf("%s%s %#4.2f \n\n", name, date, amount);
    total += amount;
    fgets(line, sizeof(line), input);   /* user field 1 - ignore */
    fgets(line, sizeof(line), input);   /* user field 2 - ignore */
    fgets(line, sizeof(line), input);   /* user field 3 - ignore */
    fgets(line, sizeof(line), input);   /* user field 4 - ignore */
    fgets(line, sizeof(line), input);   /* user field 5 - ignore */
  }
  printf("\nGrand total = %#6.2f \n", total);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.