L-34 MCS 275 Wed 4 Apr 2001
Below are some extented versions of the programs we discussed in class.
/* L-34 MCS 275 Wed 4 Apr 2001 : logging phone calls into file */
/* The program below can be executed with a command line argument.
This argument is the name of the log file. If there is no argument,
then the user will be prompted to specify a file name.
If the given file already exists, then new data will be added,
otherwise a new log file will be created.
After updating, the content of the log file will be printed on screen. */
#include <stdio.h>
#include <string.h>
#include <time.h>
void update ( FILE *log );
/* updates the file and prints the current content */
void print ( FILE *file );
/* prints the content of the file on screen */
int main ( int argc, char* argv[] )
{
char message[80];
char filename[80];
FILE *infile,*logfile;
printf("\nLogging phone calls into a file.\n\n");
if (argc > 1)
strcpy(filename,argv[1]);
else
{
printf("Give the name of the log file : ");
scanf("%s", filename);
}
infile = fopen(filename,"r"); /* check whether file exists */
if (infile == NULL)
{
sprintf(message,"%s%s%s%s%s%s",
"File \"", filename, "\" does not exist.\n",
"Will create the file \"", filename, "\".\n");
logfile = fopen(filename,"w+"); /* open for writing and reading */
}
else
{
sprintf(message,"%s%s%s%s%s%s",
"File \"", filename, "\" found.\n",
"Will append to the file \"", filename, "\".\n");
fclose(infile);
logfile = fopen(filename,"a+"); /* open for appending and reading */
}
printf("%s", message);
update(logfile);
fclose(logfile);
return 0;
}
void update ( FILE *log )
{
int area,numb1,numb2;
float duration;
time_t current;
char *timestring;
printf("\n");
printf("Give area code : "); scanf("%d", &area);
printf("first 3 digits : "); scanf("%d", &numb1);
printf("last 4 digits : "); scanf("%d", &numb2);
printf("Give duration (minutes.seconds) : ");
scanf("%f", &duration);
fprintf(log,"%d %d %d %.2f", area, numb1, numb2, duration);
current = time(NULL); /* make a time stamp */
timestring = ctime(¤t); /* contains current date and time */
fprintf(log," %s", timestring);
rewind(log);
printf("The current content of the log file :\n");
print(log);
}
void print ( FILE *file )
{
char c;
while (fscanf(file,"%c", &c) == 1)
printf("%c", c);
}
The file created the program above is used as input in the following program.
/* L-34 MCS 275 Wed 4 Apr 2001 : making a phone bill */
/* The program below can take one argument on the command line,
which contains the logged calls. If this argument is not given,
then the user is asked to provide a file name.
The phone bill is added to the log file. */
#include <stdio.h>
#include <string.h>
void billing ( FILE *bill, float local_rate, float other_rate );
/* the bill is computed with the given rates for local and other calls,
and is appended to the given file */
void print ( FILE *file );
/* prints the content of the file on screen */
int main ( int argc, char* argv[] )
{
char message[80];
char filename[80];
FILE *infile,*billfile;
float locrate,othrate;
printf("\nMaking a phone bill from the logged phone calls.\n\n");
if (argc > 1)
strcpy(filename,argv[1]);
else
{
printf("Give the name of the log file : ");
scanf("%s", filename);
}
infile = fopen(filename,"r"); /* check whether file exists */
if (infile == NULL)
printf("File \"%s\" does not exist. Please try again...\n", filename);
else
{
sprintf(message,"%s%s%s%s%s%s",
"File \"", filename, "\" found.\n",
"Will append the phone bill to \"", filename, "\".\n");
fclose(infile);
billfile = fopen(filename,"r+"); /* open for reading and writing */
printf("%s\n", message);
printf("Give rate for local calls : "); scanf("%f", &locrate);
printf("Give rate for other calls : "); scanf("%f", &othrate);
billing(billfile,locrate,othrate);
printf("The phone bill :\n");
rewind(billfile);
print(billfile);
fclose(billfile);
}
return 0;
}
void billing ( FILE *bill, float local_rate, float other_rate )
{
int area,numb1,numb2;
float duration;
int c;
int local = 0;
float payloc,durloc = 0.0;
int other = 0;
float payoth,duroth = 0.0;
while (fscanf(bill,"%d %d %d %f", &area, &numb1, &numb2, &duration) == 4)
{
if (area == 312)
{
local++;
durloc = durloc + duration;
}
else
{
other++;
duroth = duroth + duration;
}
fscanf(bill,"%*[^\n]"); /* skip rest of the line */
c = getc(bill); /* skip the new line symbol */
}
while (c = getc(bill) != EOF); /* skip garbage until EOF */
fprintf(bill,"Total number of calls : %d\n", local+other);
fprintf(bill," %d local call(s)", local);
fprintf(bill," for %5.2f minutes", durloc);
payloc = durloc*local_rate;
fprintf(bill," at %.2f a minute : $%.2f.\n", local_rate, payloc);
fprintf(bill," %d other call(s)", other);
fprintf(bill," for %5.2f minutes", duroth);
payoth = duroth*other_rate;
fprintf(bill," at %.2f a minute : $%.2f.\n", other_rate, payoth);
fprintf(bill,"Total amount due : $%.2f.\n", payloc+payoth);
}
void print ( FILE *file )
{
char c;
while (fscanf(file,"%c", &c) == 1)
printf("%c", c);
}