C প্রোগ্রামিং ও File অপারেশন

Submitted by administrator on Mon, 01/02/2012 - 16:33

In Chapter 5, "Reading from and Writing to Standard I/O," you learned how to read or write characters through standard input or output. In this lesson you'll learn to read data from or write data to disk files. The following topics are discussed in this lesson:

    Files and streams
    Opening a file with fopen()
    Closing a file with fclose()
    The fgetc() and fputc() functions
    The fgets() and fputs() functions
    The fread() and fwrite() functions
    The feof() function


Summary

  •     In C, a file can refer to a disk file, a terminal, a printer, or a tape drive.
  •     The data flow you transfer from your program to a file, or vice versa, is called a stream.
  •     A stream is a series of ordered bytes.
  •     Not like a file, a stream is device-independent.
  •     There are two stream formats: text stream and binary stream.
  •     The file position indicator in the FILE structure points to the position in a file where data will be read from or written to.
  •     The fopen() function is used to open a file and associate a stream to the opened file.
  •     You can specify different modes for opening a file.
  •     The fclose() function is responsible for closing an opened file and disassociating a stream with the file.
  •     The fgetc() and fputc() functions read or write one character at a time.
  •     The fgets() and fputs() functions read or write one line at a time.
  •     The fread() and fwrite() functions read or write one block of data at a time.
  •     The feof() function can determine when the end of a file has been reached.
  •     In a binary file, the feof() function should be used to detect EOF.

 

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

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.

Question and Answer

    Q Why is random access to a disk file necessary?