1 of 12

CSO101: Computer Programming

Lecture – 29

O.S.L. Bhavana

2 of 12

Files

  • Programs may directly read or write data to files in the system
  • So far we have been reading/writing data to command prompt – called standard input /output/error stream
  • To work with files we need file streams
  • Here is an example program to read from files

3 of 12

Files

  • Data about file (ex text file) is information like the content of the file, how many characters it has, where does the file end, date of file creation etc
  • FILE is the structure used to store data about a file
  • It is defined in the header file stdio.h
  • Operations on files include opening/reading/writing-to/closing a file, moving to a particular location in file, creating a new file

4 of 12

Files

  • fopen is a library function that takes a file name and the mode of access (read/write etc) as input
  • Both the file name (input.txt) and the mode (r –reading mode) are specified as strings.
  • fopen
    • searches for the file specified, loads it and
    • It sets up the file position indicator to the first character of the file
    • returns a pointer to the file

5 of 12

Files: fopen

  • In case fopen fails (for reasons like file not found, file not writable etc) it returns NULL
  • Always check if the returned file pointer is null or not before using

6 of 12

Files

  • fgetc is used to read a character from a file (whose pointer is given as input) similar to getchar for stdin
  • fgetc reads a character from the current “file position indicator”, advances the indicator to next character and returns the character read
  • A file implicitly ends with the EOF character (usually ASCII value -1) to signify the end of contents of the file

7 of 12

Files: fclose

fclose :

  • Any data remaining in the buffer is physically written to the disk.
  • The memory used by the FILE structure is released.
  • The "lock" on the file is released so other programs can use it.

8 of 12

Files

  • The while loop reads the contents of the file “input.txt” character by character until the EOF of the file is encountered
  • The while loop also prints the characters read from file to the standard output stream stdout
  • fclose takes in a file pointer as input and closes the file corresponding to it.
  • The output of the program is

9 of 12

Files

  • Here is a program that reads from a file and writes to a file
  • The mode of accessing the file is “w” (write)
  • When a file is opened in the write mode, fopen creates a newfile with the name if the file doesn’t exist
  • fputc takes in a character, a file pointer as input and writes the input character to the file

10 of 12

Practice

Write a program that reads all characters of a file and writes these characters in reverse order to a new file.

11 of 12

File Modes

Mode

Operations

“r”

Reading from a file – sets up a pointer to the first character in it

“w”

Writing to a file. If the file already exists the file will be over-written, else a file with the name will be created

“a”

Opens a file for writing and sets up a pointer to the last character in it. If the file doesn’t exist a new file will be created.

Used to append contents at the end of a file

“r+”

Reading existing content, writing new content, modifying existing content

“w+”

Writing new content, reading the new content and modifying existing contents of file

“a+”

Reading existing content, appending new content at the end of file – cannot modify existing content

Reference: Let us C

fopen returns a null pointer if the file cannot be opened (irrespective of the mode)

12 of 12

File Modes