Unit V
Unit V
topics:
Definition of Files in C:
Types of Files in C:
1. Text Files:
o These are files in which data is stored as human-readable text. They are used
to store plain text.
o Operations on text files are done using fopen(), fread(), fwrite(),
fclose(), etc.
2. Binary Files:
o These are files where data is stored in a binary format. Binary files are used to
store structured data.
o Operations on binary files are done using fopen(), fread(), fwrite(),
fclose(), etc.
File Modes:
Reading data from a text file where each line is processed one by one.
o
Writing logs to a file where each log entry is appended.
o
2. Random Access File Processing:
o Allows you to read or write data at any position within the file, i.e., the file
pointer can be moved to any part of the file using fseek().
o It is more efficient for certain applications, such as databases or systems that
store records at arbitrary locations.
In sequential access, the file is accessed from the beginning to the end in a linear
manner. The file pointer moves to the next record/byte after each read/write operation.
This method is simple and easy to implement but inefficient for accessing specific
records in large files.
Characteristics:
Random access allows you to move the file pointer directly to any part of the file
without having to read through the file sequentially.
This access method is useful for applications where records can be randomly
accessed, such as in databases or file systems.
Characteristics:
You can access any part of the file at any time, making it more efficient than
sequential access for specific read/write operations.
Uses fseek() to move the pointer and ftell() to find the current pointer position.
struct Record {
int id;
char name[20];
};
fseek(file, 0, SEEK_SET);
fwrite(&record1, sizeof(struct Record), 1, file);
fseek(file, sizeof(struct Record), SEEK_SET); // Move to the second record
fwrite(&record2, sizeof(struct Record), 1, file);
fclose(file);
Command line arguments are passed to a C program at the time of execution via the
terminal. The arguments are passed as an array of strings (argv[]), and the number of
arguments is stored in argc.
argc holds the number of arguments passed to the program (including the program
name itself).
argv[] is an array of strings containing each argument passed from the command
line.
The program can use argc to check how many arguments were passed, and then use
argv[] to access each argument.
Command line arguments are useful for passing file names, configuration settings, or
other parameters.
#include <stdio.h>
return 0;
}
Conclusion
File Processing in C involves working with files for storing and retrieving data.
Sequential Access processes files line by line, making it simpler but less efficient for
random access needs.
Random Access allows direct access to any part of the file, making it more efficient
for certain tasks, such as database management.
Command Line Arguments enable flexible input from the user or other programs,
allowing your program to work with various inputs at runtime.
Concept:
Files are used to store data on a permanent basis. C provides standard library functions for
working with files: opening, reading, writing, and closing.
Example from T1, Chapter 15, pp. 420-440 (C Programming: "Programming in C"
by Reema Thareja)
int main() {
FILE *file;
char str[] = "This is an example of file handling in C.";
return 0;
}
Explanation:
Concept:
Example from R3, Chapter 11, pp. 500-520 (C Programming: "C How to Program"
by Deitel & Deitel)
Program (Sequential Access - Writing and Reading):
#include <stdio.h>
int main() {
FILE *file;
char text[] = "Sequential access example.\n";
return 0;
}
#include <stdio.h>
struct Record {
int id;
char name[50];
};
int main() {
FILE *file;
struct Record record;
return 0;
}
Explanation:
In sequential access, the data is read and written in order, one after the other.
In random access, fseek() is used to move the pointer to a specific record, and
fread()/fwrite() allow direct reading and writing.
Concept:
Sequential access is used when data needs to be processed in a sequential manner, such as
reading or writing records one by one.
Example from T1, Chapter 15, pp. 440-460 (C Programming: "Programming in C"
by Reema Thareja)
#include <stdio.h>
int main() {
FILE *file;
char line[100];
return 0;
}
Explanation:
The program uses fprintf() to write lines to a file and fgets() to read lines from
the file sequentially.
The pointer moves automatically to the next line after reading.
Concept:
Random access allows reading and writing files at arbitrary positions using fseek(), making
it ideal for databases and large files that require frequent updates or lookups.
Example from R1, Chapter 10, pp. 550-575 (C Programming: "Let Us C" by
Yashwant Kanetkar)
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
FILE *file;
struct Employee e;
fclose(file);
return 0;
}
Explanation:
The program writes and updates specific records in the file using random access.
fseek() is used to move the pointer to the desired position, and fwrite() is used to
overwrite the record.
Concept:
Command line arguments are parameters passed to the program when it is executed. These
arguments are used to provide input values to the program, like filenames or numerical data.
Example from Book:
Example from T1, Chapter 15, pp. 460-470 (C Programming: "Programming in C"
by Reema Thareja)
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Explanation:
The program takes two command line arguments, converts them to integers using
atoi(), and calculates their sum. If the user doesn't provide the correct number of
arguments, it displays a usage message.
Here are the technical interview questions along with their answers, company names, and
years, based on the topics of File Processing in C (Sequential Access, Random Access,
Command Line Arguments):
1. Files in C
Question:
Explain how file handling works in C. What are the common file modes used in C, and
how do you open and close files?
Answer:
File handling in C involves reading from and writing to files stored on secondary storage
(e.g., hard drives). Common functions used in file handling are:
File Modes:
Example Code:
Question:
What is the difference between sequential access and random access file processing in
C? Explain with an example.
Answer:
Sequential Access:
Data is read or written one record at a time in the order it appears in the file.
The file pointer moves automatically after each read or write operation.
Random Access:
Data can be accessed directly at any position in the file using fseek().
It is used for tasks like databases where you need to access or modify specific records.
Question:
What is sequential access file processing in C? How would you implement a program to
read data from a sequential file?
Answer:
#include <stdio.h>
int main() {
FILE *file;
char line[100];
return 0;
}
Company: TCS
Year: 2021
Question:
Explain random access file processing in C. How can you modify specific records in a
file using random access?
Answer:
Random Access allows you to move the file pointer to any position within the file using
fseek(). This is useful for updating or reading specific records.
Key Functions:
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
FILE *file;
struct Employee e;
Question:
What are command line arguments in C? How can you pass arguments to a C program
and use them in the program?
Answer:
Command Line Arguments are parameters passed to the program at runtime. They are
passed to the program through the terminal and accessed using the argc (argument count)
and argv[] (argument vector) variables.
argc: The number of arguments passed to the program (including the program name).
argv[]: An array of strings containing the arguments passed.
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Explanation:
The program expects two command-line arguments (numbers). The atoi() function is used
to convert these arguments from strings to integers.
The program calculates and prints the sum of the two numbers.
Company: Accenture
Year: 2021