a. Submit this homework electronically. Name your program hw14a.c b. Don’t do wh
ID: 3625580 • Letter: A
Question
a. Submit this homework electronically. Name your program hw14a.c
b. Don’t do what the problem states. We’ll do something different.
c. Let the input file, hw14a.in, contain:
Boomer, Baby 1
Lady, Old 101
Ryan, Elizabeth 62
McIntyre, Osborne 84 ...
/* plus the other 4 names in the text */
d. Read in the list of names, one row at a time, using fgets()until the null pointer is reached (an address of 0), which occurs at the end of the file. The C code using fgets() will look something like:
status = fgets(line, MAX, inp);
where line is the input string, MAX is the maximum number of characters, inp
is the input file pointer, and status is a character pointer.
e. Use sscanf() to separate a line of data into a last name (string), first name
(string), and an age (integer).
f. Use sprintf() to recreate a line of output data of the format:
1 Baby Boomer
101 Old Lady
62 Elizabeth Ryan
84 McIntyre, Osborne
......
g. Then print this output line to the output file, hw14a.out, one line at a time. It should look like what is displayed in part f.
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include<string.h>
int main()
{
char name[20];
char name1[20];
int p;
char c[60];
char out[40];
FILE *f1;
FILE *f2;
f2 = fopen("hw14a.out","w");
f1=fopen("hw14a.in","r");
while(!feof(f1))
{
fgets(c,60,f1);
sscanf(c,"%s %s %d ", name,name1,&p);
name[strlen(name)-1]='';
sprintf(out,"%d %s %s",p,name,name1);
fprintf(f2,"%s ",out);
}
fclose(f1);
fclose(f2);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.