The scenario goes like this,
A medical center needs to store appointment details in a text file called appointment.dat.
It includes the Patient name and the Appointment type. The appointment types can either be 'Consulting', 'Scanning' or 'Testing'. Only the first letter is stored in the file.
The requirement is to,
- Create the
appointment.datfile - Get 5 patient details through keyboard input and write the data into the
appointment.datfile under the given sample format.
Dave C
Ryan T
Mary S
George C
Julian S
- Read the
appointment.datfile and calculate and display the Number of Patients under the given format.
Appointment Type Number of patients
Consulting 2
Scanning 2
Testing 1
Here's the code I tried,
#include <stdio.h>
int main()
{
int cCount, sCount, tCount;
char name, chan, C, S, T, c, s, t;
chan = " ";
cCount = sCount = tCount = 0;
FILE *cPtr;
cPtr = fopen ("appointment.dat", "r");
while (!feof(cPtr))
{
fscan (cPtr, "%s", &chan);
if (chan == C)
cCount++;
else if (chan == S)
sCount++;
else if (chan == T)
tCount++;
}
printf ("Appointment Type Number of patients\n");
printf ("Consulting %d \n");
printf ("Scanning %d \n");
printf ("Testing %d \n");
return 0;
}
I'm having trouble getting the displaying part right. The Number of Patients displays some addresses instead of the patient count.
How do I modify the code to get the patient count right?
You have fallen into one of the first pitfalls most new C-programmers fall into. Why is while ( !feof (file) ) always wrong? On your call to
fscan (cPtr, "%s", &chan);(which should befscanf), for the last line of input, the read succeeds andEOFis not set. You testwhile (!feof(cPtr))-- and it's NOT. You loop again and reachfscan (cPtr, "%s", &chan);which now fails due to an input-failure andEOFis returned -- but you blindly proceed to checkif (chan)which may fail at this point or may appear to work correctly (adding an additional erroneous count to the variable corresponding to whatever the last value ofchanwas). 1You further invoke Undefined Behavior in your use of
printfby failing to provide any argument for the"%d"conversion specifier contained in the format string, e.g.C11 Standard - 7.21.6.1 The fprintf function(p2) (this explains your "The Number of Patients displays some addresses instead of the patient count.")
When you are reading a line-of-input at a time, use a line-oriented input function like
fgets()or POSIXgetline()to read a complete line into a sufficiently sized array, and then usesscanf()to separate the array into the needed values, e.g.You use the return of the read function itself to control the continuation of the read-loop. You cannot use any user-input function correctly without Checking the
return.Now you can check the
type. A simple way is to use aswitch()statement -- though there is nothing wrong with a sequence ofif()statements. You can use aswitch()similar to:Putting it altogether, and allowing the filename to be passed as the first argument to the program (or read from
stdinby default if no filename is given), you could do:(note: you only need one
printfstatement for each contiguous output. The C compiler will concatenate all adjacent whitespace separated strings, e.g."...""..."into a single string)Example Use/Output
Simply providing your input to the program on
stdinusing a bash heredoc, you would get:Reading From A File
With your data in the file
dat/appointment_types.txt, e.g.Your use and output would be:
Look things over and let me know if you have further questions.
Footnotes:
1. The C-Standard does not define what
chanwill hold after an input-failure occurs. C11 Standard - 7.21.6.2 The fscanf function(p9) The behavior is simply undefined becausechanis indeterminate at this point and used while it has an indeterminate value. C11 Standard - J.2 Undefined Behavior"The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8)."and see discussion Undefined, unspecified and implementation-defined behavior and What is indeterminate behavior in C++ ? How is it different from undefined behavior?