C Programming · Unit 10 · 4 hrs
File Handling in C
Exam-focused notes for File Handling in C (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.
What this unit covers
- Concept of File
- Opening and closing of File
- Input Output Operations in File
- Random access in File
- Error Handling in Files
Opening and closing of File
Explain different file opening modes. [5]
When working with files in C, a file must be opened before any I/O operations can be performed on it. The fopen() function is used to open a file, and it requires a mode string that specifies how the file should be accessed. Syntax: --- Mode Name Descriptio...
Full solved answer →Input Output Operations in File
Describe the different types of I/O functions used in file handling with syntax. [5]
File handling in C provides several categories of I/O functions to read from and write to files. These are defined in <stdio.h. --- Used to read/write a single character at a time. Writes a single character to a specified file and increments the file positi...
Full solved answer →Suppose a file named 'Num.txt' contains a list of integers. Write a program to extract the prime numbers only from that file and write them on 'Prime.txt' file. [5]
- Read integers from Num.txt using fscanf() - Check each integer for primality using a loop - Write prime numbers to Prime.txt using fprintf() - Use FILE pointer and formatted I/O --- --- Step Description ------------------- fopen("Num.txt", "r") Opens Num....
Full solved answer →Write a program that simply reads data from a file. [5]
To read data from a file in C, the following steps are performed: 1. Declare a file pointer of type FILE 2. Open the file using fopen() in read mode "r" 3. Read data from the file using file input functions (e.g., fgetc(), fscanf(), fgets()) 4. Close the fi...
Full solved answer →Explain different file I/O functions with example. [5]
File I/O functions allow a C program to read data from and write data to files stored on disk. These functions are declared in the header file <stdio.h. --- These functions read or write one character at a time. Writes a single character to a specified file...
Full solved answer →Write a program to read and print data stored in a file input.txt. [5]
- A file is a collection of related data placed on disk. - fopen() is used to open a file with a specified mode. - Mode "r" opens an existing file for reading only; previous data is not erased. - fgetc() reads a single character from a file and increments t...
Full solved answer →Concept of File
Why do we need data files?What are the different file opening modes?Write a program that reads data from a file "input.txt" and writes to "output.txt" file.[2+3+5]
--- (a) Why Do We Need Data Files? When a program runs, all data is stored in main memory (RAM), which is volatile -- it is lost when the program ends or the computer is switched off. Data files solve this problem by providing permanent storage on secondary...
Full solved answer →Write short note on: a. Benefits of data files b. Graphics functions. [2.5+2.5]
--- A file is a collection of related data placed on the disk. C supports the concept of files through which data can be stored on a disk or secondary storage device, and the stored data can be read whenever required. The key benefits of using data files ar...
Full solved answer →Make Unit 10 stick
Practice CSC115 with flashcards & quizzes