C programming

Exercises : Answer the following Question

To help solidify your understanding of this lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, “Answers to Quiz Questions and Exercises.”

What do the following expressions do?

fopen(“test.bin”, “r+b”)
fopen(“test.txt” “a”)
fopen(“test.ini”, “w+”)

What’s wrong with the following code segment?

Question and Answer

Q What are the differences between a text stream and a binary stream?

A A text stream is a sequence of characters that may not have a one-to-one relationship with the data on the device. Text streams are normally used for textual data, which have a consistent appearance from one environment to another, or from one machine to another. A binary stream, on the other hand, is a sequence of bytes that has a one-to-one correspondence to those on the device. Binary streams are primarily used for nontextual data that is needed to keep the exact contents on the device.

Reading and Writing Disk Files

Reading and Writing Disk Files

The program in Listing 21.1 does not do anything with the text file, haiku.txt, except open and close it. In fact, there are two pieces of Japanese haiku, written by Sodo and Chora, saved in the haiku.txt file. So how can you read them from the file?

In C, you can perform I/O operations in the following ways:

Read or write one character at a time.
Read or write one line of text (that is, one character line) at a time.
Read or write one block of characters at a time.

Files Versus Streams

Files Versus Streams

The C language provides a set of rich library functions to perform input and output (I/O) operation. Those functions can read or write any type of data to files. Before we go any further in discussing the C I/O functions, let’s first understand the definitions of files and streams in C.

Exercises : Answer the following Question

To help solidify your understanding of this hour’s lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, “Answers to Quiz Questions and Exercises.”

Of the following two statements, which one is the declaration of a union and which one is the definition of union variables?

union a_union {
int x;
char y;
};