Read Sams Teach Yourself C in 24 Hours Online
Authors: Tony. Zhang
printf(“The content assigned to the union separately:\n”);
/* access to name */
strcpy(dish.name, “Sweet and Sour Chicken”);
/* access to price */
dish.price = 9.95;
printf(“Dish Name: %s\n”, dish.name);
printf(“Dish Price: %5.2f\n”, dish.price);
return 0;
}
2. The following is one possible solution:
/* 20A02.c */
#include
union employee {
int start_year;
int dpt_code;
int id_number;
};
void DataDisplay(union employee u);
33 067231861x AppxB 4.10.2000 11:05 AM Page 487
Answers to Quiz Questions and Exercises
487
main(void)
{
union employee info;
/* initialize start_year */
info.start_year = 1997;
DataDisplay(info);
/* initialize dpt_code */
info.dpt_code = 8;
DataDisplay(info);
/* initialize id */
info.id_number = 1234;
DataDisplay(info);
return 0;
}
/* function definition */
void DataDisplay(union employee u)
{
printf(“Start Year: %d\n”, u.start_year);
printf(“Dpt. Code: %d\n”, u.dpt_code);
B
printf(“ID Number: %d\n”, u.id_number);
}
The output of the program is
OUTPUT
Start Year: 1997
Dpt. Code: 1997
ID Number: 1997
Start Year: 8
Dpt. Code: 8
ID Number: 8
Start Year: 1234
Dpt. Code: 1234
ID Number: 1234
3. The following is one possible solution:
/* 20A03.c */
#include
#include
struct survey {
char name[20];
union {
char state[32];
char country[32];
} place;
};
33 067231861x AppxB 4.10.2000 11:05 AM Page 488
488
Appendix B
void DataEnter(struct survey *s);
void DataDisplay(struct survey *s);
main(void)
{
struct survey citizen;
DataEnter(&citizen);
DataDisplay(&citizen);
return 0;
}
/* definition of DataDisplay() */
void DataDisplay(struct survey *ptr)
{
printf(“\nHere is what you entered: \n”);
printf(“Your name is %s.\n”, ptr->name);
printf(“You are from %s.\n”, ptr->place.state);
printf(“\nThank you!\n”);
}
/* definition of DataEnter() */
void DataEnter(struct survey *ptr)
{
char is_yes[4];
printf(“Please enter your name:\n”);
gets(ptr->name);
printf(“Are you a U. S. citizen? (Yes or No)\n”);
gets(is_yes);
if ((is_yes[0] == ‘Y’) ||
(is_yes[0] == ‘y’)){
printf(“Enter the name of the state:\n”);
gets(ptr->place.state);
} else {
printf(“Enter the name of your country:\n”);
gets(ptr->place.country);
}
}
The following is the output of the program on my machine:
OUTPUT
Please enter your name:
Tony
Are you a U. S. citizen? (Yes or No)
Yes
Enter the name of the state:
Texas
33 067231861x AppxB 4.10.2000 11:05 AM Page 489
Answers to Quiz Questions and Exercises
489
Here is what you entered:
Your name is Tony.
You are from Texas.
Thank you!
4. The following is one possible solution:
/* 20A04.c */
#include
#include
struct bit_field {
int yes: 1;
};
struct survey {
struct bit_field flag;
char name[20];
union {
char state[32];
char country[32];
} place;
};
B
void DataEnter(struct survey *s);
void DataDisplay(struct survey *s);
main(void)
{
struct survey citizen;
DataEnter(&citizen);
DataDisplay(&citizen);
return 0;
}
/* function definition */
void DataEnter(struct survey *ptr)
{
char is_yes[4];
printf(“Please enter your name:\n”);
gets(ptr->name);
printf(“Are you a U.S. citizen? (Yes or No)\n”);
gets(is_yes);
if ((is_yes[0] == ‘Y’) ||
(is_yes[0] == ‘y’)){
printf(“Enter the name of the state:\n”);
gets(ptr->place.state);
33 067231861x AppxB 4.10.2000 11:05 AM Page 490
490
Appendix B
ptr->flag.yes = 1;
} else {
printf(“Enter the name of your country:\n”);
gets(ptr->place.country);
ptr->flag.yes = 0;
}
}
/* function definition */
void DataDisplay(struct survey *ptr)
{
printf(“\nHere is what you’ve entered:\n”);
printf(“Name: %s\n”, ptr->name);
if (ptr->flag.yes)
printf(“The state is: %s\n”,
ptr->place.state);
else
printf(“Your country is: %s\n”,
ptr->place.country);
printf(“\nThanks and Bye!\n”);
}
Hour 21, “Reading and Writing with Files”
Quiz
1. The first expression tries to open an existing binary file called test.bin for reading and writing. The second expression tries to open an existing text file called test.txt for appending. The last expression tries to create a text file, called test.ini, for reading and writing.
2. The fopen() function returns a null pointer when an error occurs during the procedure of opening a file. It’s not legal to do any reading or writing with a null file pointer. Therefore, the code is wrong because it calls fgetc() when fopen() returns a null pointer.
3. The mode is set to read only, but the code tries to write a character to the opened file by calling the fputc() function.
4. The code still reads a text file by using the file pointer fptr1, even though the file pointer fptr1 has been closed.
Exercises
1. The following is one possible solution:
/* 21A01.c */
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 491
Answers to Quiz Questions and Exercises
491
enum {SUCCESS, FAIL};
int CharRead(FILE *fin);
main(void)
{
FILE *fptr;
char filename[]= “haiku.txt”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “r”)) == NULL){
printf(“Cannot open %s.\n”, filename);
reval = FAIL;
} else {
printf(“\nThe total character number is %d.\n”,
CharRead(fptr));
fclose(fptr);
}
return reval;
}
/* definition of CharRead() */
int CharRead(FILE *fin)
B
{
int c, num;
num = 0;
while ((c=fgetc(fin)) != EOF){
putchar(c);
++num;
}
return num;
}
2. The following is one possible solution:
/* 21A02.c */
#include
#include
enum {SUCCESS, FAIL, MAX_LEN = 80};
void LineWrite(FILE *fout, char *str);
main(void)
{
FILE *fptr;
char str[MAX_LEN+1];
char filename[32];
int reval = SUCCESS;
33 067231861x AppxB 4.10.2000 11:05 AM Page 492
492
Appendix B
printf(“Please enter the file name:\n”);
gets(filename);
printf(“Enter a string:\n”);
gets(str);
if ((fptr = fopen(filename, “w”)) == NULL){
printf(“Cannot open %s for writing.\n”, filename);
reval = FAIL;
} else {
LineWrite(fptr, str);
fclose(fptr);
}
return reval;
}
/* definition of LineWrite() */
void LineWrite(FILE *fout, char *str)
{
fputs(str, fout);
printf(“Done!\n”);
}
3. The following is one possible solution:
/* 21A03.c */
#include
enum {SUCCESS, FAIL};
void CharWrite(FILE *fout, char *str);
main(void)
{
FILE *fptr;
char filename[]= “test_21.txt”;
char str[]= “Disk file I/O is fun.”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “w”)) == NULL){
printf(“Cannot open %s.\n”, filename);
reval = FAIL;
} else {
CharWrite(fptr, str);
fclose(fptr);
}
return reval;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 493
Answers to Quiz Questions and Exercises
493
/* function definition */
void CharWrite(FILE *fout, char *str)
{
int i, c;
i = 0;
while ((c=str[i]) != ‘\0’){
putchar(c);
fputc(c, fout);
i++;
}
}
4. The following is one possible solution:
/* 21A04.c */
#include
#include
enum {SUCCESS, FAIL};
void BlkWrite(FILE *fout, char *str);
main(void)
{
B
FILE *fptr;
char filename[]= “test_21.txt”;
char str[]= “Disk file I/O is tricky.”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “w”)) == NULL){
printf(“Cannot open %s.\n”, filename);
reval = FAIL;
} else {
BlkWrite(fptr, str);
fclose(fptr);
}
return reval;
}
/* function definition */
void BlkWrite(FILE *fout, char *str)
{
int num;
num = strlen(str);
fwrite(str, sizeof(char), num, fout);
printf(“%s\n”, str);
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 494
494
Appendix B
Hour 22, “Using Special File Functions”
Quiz
1. Yes. The two statements are equivalent.
2. No. The two statements are not equivalent, unless the current file position indicator is indeed at the beginning of the file.
3. The scanf() function reads from the test.txt file, instead of the default input stream, because the freopen() function has redirected the input stream and associated it with the test.txt file.
4. The four double data items together are going to take 32 bytes in the binary file, if the size of the double data type is eight bytes long.
Exercises
1. The following is one possible solution:
/* 22A01.c */
#include
enum {SUCCESS, FAIL, MAX_LEN = 80};
void PtrSeek(FILE *fptr);
long PtrTell(FILE *fptr);
void DataRead(FILE *fptr);
int ErrorMsg(char *str);
main(void)
{
FILE *fptr;
char filename[]= “LaoTzu.txt”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “r”)) == NULL){
reval = ErrorMsg(filename);
} else {
PtrSeek(fptr);
fclose(fptr);
}
return reval;
}
/* function definition */
void PtrSeek(FILE *fptr)
{
long offset1, offset2, offset3;
33 067231861x AppxB 4.10.2000 11:05 AM Page 495
Answers to Quiz Questions and Exercises
495
offset1 = PtrTell(fptr);
DataRead(fptr);
offset2 = PtrTell(fptr);
DataRead(fptr);
offset3 = PtrTell(fptr);
DataRead(fptr);
printf(“\nRe-read the paragraph:\n”);
/* re-read the third sentence */
fseek(fptr, offset3, SEEK_SET);
DataRead(fptr);
/* re-read the second sentence */
fseek(fptr, offset2, SEEK_SET);
DataRead(fptr);
/* re-read the first sentence */
fseek(fptr, offset1, SEEK_SET);
DataRead(fptr);
}
/* function definition */
long PtrTell(FILE *fptr)
B
{
long reval;
reval = ftell(fptr);
printf(“The fptr is at %ld\n”, reval);
return reval;
}
/* function definition */
void DataRead(FILE *fptr)
{
char buff[MAX_LEN];
fgets(buff, MAX_LEN, fptr);
printf(“%s”, buff);
}
/* function definition */
int ErrorMsg(char *str)
{
printf(“Cannot open %s.\n”, str);
return FAIL;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 496
496
Appendix B
2. The following is one possible solution:
/* 22A02.c */
#include
enum {SUCCESS, FAIL, MAX_LEN = 80};
void PtrSeek(FILE *fptr);
long PtrTell(FILE *fptr);
void DataRead(FILE *fptr);
int ErrorMsg(char *str);
main(void)
{
FILE *fptr;
char filename[]= “LaoTzu.txt”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “r”)) == NULL){
reval = ErrorMsg(filename);
} else {
PtrSeek(fptr);
fclose(fptr);
}
return reval;
}
/* function definition */
void PtrSeek(FILE *fptr)
{
long offset1, offset2, offset3;
offset1 = PtrTell(fptr);
DataRead(fptr);
offset2 = PtrTell(fptr);
DataRead(fptr);
offset3 = PtrTell(fptr);
DataRead(fptr);
printf(“\nRe-read the paragraph:\n”);
/* re-read the third sentence */
fseek(fptr, offset3, SEEK_SET);
DataRead(fptr);
/* re-read the second sentence */
fseek(fptr, offset2, SEEK_SET);
DataRead(fptr);
/* re-read the first sentence */
rewind(fptr); /* rewind the file position indicator */