Q Why is random access to a disk file necessary?
A When you want to fetch a piece of information from a large file that contains a huge amount of data, random access to the file is a more efficient way than sequential access at the file. The functions that perform random access can put the file position indicator directly at the right place in the file, and then you can simply start to fetch the required information from there. In C, the fseek() and ftell() functions are two handy functions that help you to carry out the random access operation.
Q How do you specify the format of a new disk file you're going to create by calling fopen()?
A We have to add b to the mode argument to the fopen() function to specify that the file we're going to create is a binary file. We can use "wb" to create a new file for writing and "wb+" to create a new file for writing and reading. If, however, the file to be created is a text file, no b is needed in the mode argument.
Q What is the difference between the printf() and fprintf() functions?
A Basically, the printf() and fprintf() functions can do a similar job: send the formatted data items to the output streams. However, the printf() function automatically sends formatted data to stdout, whereas the fprintf() function can be assigned a file pointer that is associated with a specified output stream.
Q Can you redirect a standard stream to a disk file?
A Yes. With the help of the freopen() function, you can redirect a standard stream and associate the stream with a disk file.
Question and Answer
Related Items
The continue Statement
The continue Statement
The break Statement
The break Statement
You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.
The switch Statement
The switch Statement
Nested if Statements
Nested if Statements
As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.
The if-else Statement
The if-else Statement