10 File Input Output And Preprocessor

C Programming · Unit 10

File Input Output and Preprocessor

Exam-focused notes for File Input Output and Preprocessor (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 14 solved past questions from this unit.

What this unit covers

  • File handling and file operations
  • File opening modes
  • Fwrite and fread functions
  • Fgets and fgetc functions
  • Formatted input output
  • Unformatted input output
  • File creation and copying
  • Merging files
  • Reading and writing numbers to files
  • Preprocessor directives
  • Macros and macro vs function
  • File inclusion

File handling and file operations

20815 marks

Write a program to write N numbers in file "number.txt" and then read it and display only even numbers. [5]

--- --- Step Operation Function Used -------------------------------- 1 Open file for writing fopen("number.txt", "w") 2 Write numbers to file fprintf(fp, "%d\n", num) 3 Close file after writing fclose(fp) 4 Open file for reading fopen("number.txt", "r") 5 ...

Full solved answer →
20805 marks

Why data file is needed? Write a program to write N numbers in file 'number.txt' and then read it and display only even numbers. [5]

Data files are needed for the following reasons: 1. Permanent Storage - Data stored in variables is lost when the program ends. Files store data permanently on disk. 2. Large Data Handling - Files allow storing and processing large amounts of data that cann...

Full solved answer →

Preprocessor directives

20805 marks

Write short notes on: a) Macro Write short notes on: b) Opening and closing file [2.5+2.5]

A macro is a preprocessor directive in C that allows a name (identifier) to be associated with a piece of code, a constant, or an expression. The C preprocessor replaces every occurrence of the macro name with its defined text before actual compilation begi...

Full solved answer →
2081.25 marks

Define pre-processor. How do you pass pointer as argument? [5]

--- A pre-processor is a program (or phase of compilation) that processes the source code before it is passed to the compiler. It handles directives that begin with the symbol. Key features: - It performs text substitution and file inclusion - It does not u...

Full solved answer →

File creation and copying

207910 marks

List the advantages and disadvantages of global variable. Assume the two data files "numl.txt" and "num2.txt" containing integers. Merge these two files to the third file named " file3.txt". Here, the integers of " file3.t-xt" must be written in sorted order.[10]

A global variable is a variable declared outside all functions, making it accessible to every function in the program. --- Advantage Explanation --------------------------- 1 Universal Accessibility Any function can read or modify the variable without recei...

Full solved answer →
20785 marks

Write a program to create a file "duplicate" that contains the exact copy of file "original". [5]

--- Step Description ------------------- fopen("original", "r") Opens the source file "original" in read mode fopen("duplicate", "w") Creates/opens the destination file "duplicate" in write mode NULL check Verifies files opened successfully; exits with erro...

Full solved answer →

File inclusion

20795 marks

How do you include a file to another file? Illustrate with an example. [5]

In C programming, the include preprocessor directive is used to include the contents of one file into another file. This is handled by the C preprocessor before actual compilation begins. When the preprocessor encounters include, it replaces that line with ...

Full solved answer →

Macros and macro vs function

20795 marks

Write short notes on: a) Macro vs Function Write short notes on: b) Break vs Continue [2.5+2.5]

Feature Macro Function --------- Definition Defined using define preprocessor directive Defined using return type, name, and body Processing Handled by preprocessor (before compilation) Handled by compiler at compile/run time Execution Inline substitution (...

Full solved answer →
20775 marks

Write short notes on: a) Macros Write short notes on: b) Null Pointers [2.5+2.5]

A macro is a preprocessor directive in C that allows the programmer to define symbolic names or constants and to create inline code substitutions before the actual compilation begins. Macros are processed by the C preprocessor using the define directive. 1....

Full solved answer →

Formatted input output

20785 marks

Explain any four input/output functions used in C language with suitable example [5]

--- C language provides several built-in I/O functions through the header file <stdio.h (Standard Input/Output Header). --- Used to display/print output to the standard output (screen). Syntax: Example: Output: My age is 20 --- Used to read/accept input fro...

Full solved answer →
05 marks

Write short notes on: i) Formatted input/output Write short notes on: ii) C Preprocessors/directives [2.5+2.5]

Formatted input/output in C refers to reading and writing data in a specific format using standard library functions defined in <stdio.h. Used to display formatted output to the standard output (screen). Syntax: Format Specifiers: Specifier Meaning --------...

Full solved answer →

File opening modes

20775 marks

Why file is used in programming? Explain different file opening modes. [5]

Files are used in programming for the following reasons: 1. Permanent/Persistent Storage: Data stored in variables and arrays is lost when the program terminates. Files allow data to be stored permanently on secondary storage (disk). 2. Large Data Handling:...

Full solved answer →

Fgets and fgetc functions

2081.210 marks

Differentiate between fgets() and fgetc(). What is the task of typedef? Write a program to define a structure Book with data members name, price and author. Display the names of the book having price greater than 1000.[10]

Feature fgets() fgetc() --------- Purpose Reads a string (line) from a file Reads a single character from a file Syntax fgets(str, n, fp) fgetc(fp) Return Type Returns char (pointer to string) Returns int (character as integer) Parameters Takes string buffe...

Full solved answer →

Fwrite and fread functions

05 marks

Explain the syntax of fwrite() function. Write a program to create a file named "student.txt" and stores record of any five students given by user. [5]

Parameters: Parameter Description ------------------------ ptr Pointer to the block of memory (data to be written) size Size in bytes of each element to be written count Number of elements to be written stream Pointer to the FILE object where data is writte...

Full solved answer →