0
1.3kviews
Discuss file I/O in C language with different library functions
1 Answer
0
28views
  1. A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data.
  2. In C language, we use a structure pointer of file type to declare a file.

    FILE *fp;

  3. C provides two different ways of file processing. These are
    1. Standard Input/output (stream I/O or high-level I/O).
    2. System-oriented Input/output (low-level I/O).
  4. The Standard I/O is very simple and most commonly used way of performing file I/O in C language.Here data is written as individual characters or as strings.
  5. The System-oriented I/O is complex to implement. The data cannot be written as individual characters, or as strings, or as formatted data. Using this approach the data must be written as a buffer full of bytes, where user needs to create this buffers manually.
  6. C provides a number of functions that helps to perform basic file operations. Following are the functions
    1. fopen(): This command is used to open a particular file. General Syntax : *fp = FILE *fopen(const char *filename, const char *mode); Example fp = fopen("one.txt", "w"); Here filename is "one.txt" and mode w specifies the write mode.
    2. fclose(fptr): This command is used to close a file. General Syntax : int fclose( FILE *fp ); Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h
    3. getc (): Used to read from the file.(SingleCharacter) char input = getc(fp); where fp is file pointer.
    4. putc(): Used to write into the file. (SingleCharacter) putc('c',fp); where character 'c' is written in file pointed by file pointer fp;
Please log in to add an answer.