Computer Science Unit-5 Sem 1
Computer Science Unit-5 Sem 1
Unit - 5
Question-1
What is File? What are the types of files available in C? What operations may
be performed on Files in C?
Answer-1
File:
The File is a data structure that is used to store data given permanently. It is
must to store data permanently for real life applications.
There are three types of files in C:
1. Text files
2. Variable record length files
3. Fixed record length files or constant record length files
Text files:
In these files the data is written as stream of characters. It is also read as
stream of characters.
For example: following is an example of text file-
Answer-2
#include<stdio.h>
#include<conio.h>
void main( )
{
char c;
FILE *ptr;
ptr=fopen(“test.dat”, “w”);
printf(“\nEnter text and to stop press Enter key”);
do
{
c=getchar( );
putc(c, ptr);
}while(c!=’\t’);
fclose(ptr);
ptr=fopen(“test.dat”, “r”);
do
{
c=getc(ptr);
putchar(c);
}while(c!=’\t’);
fclose(ptr);
getch( );
}
Question-3
Write a program that accepts Name, Basic, DA, and HRA of some employees,
write it in a file (variable record length file) and after that also print all the
records.
Answer-3
#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *ptr;
char name[30];
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
3
Unit - 5
int basic, hra, ch=1;
float da;
ptr=fopen(“pay.dat”, “w”);
while(ch!=0)
{
printf(“\nEnter Name”);
scanf(“%[^\n]”, name);
printf(“\nEnter Basic pay”);
scanf(“%d”, &basic);
printf(“\nEnter DA”);
scanf(“%f”, &da);
printf(“\nEnter HRA”);
scanf(“%d”, &hra);
printf(“\nEnter 0 to stop”);
scanf(“%d”, &ch);
}
fclose(ptr);
ptr=fopen(“pay.dat”, “r”);
while(feof(ptr)= =0)
{
fscanf(ptr, “%s”, name);
fscanf(ptr, “%d”, &basic);
fscanf(ptr, “%f”, &da);
fscanf(ptr, “%d”, &hra);
void main( )
{
int ch=1;
FILE *ptr ;
ptr=fopen(“marks.dat”, “w”);
while(ch!=0)
{
printf(“\nEnter Roll No”);
scanf(“%d”, &std.rollno);
printf(“\nEnter Name”);
scanf(“%[^\n]”, std.name);
printf(“\nEnter Marks”);
scanf(“%d”, &std.marks);
fwrite(std, sizeof(std), 1, ptr);
printf(“Enter 0 to stop”);
scanf(“%d”, &ch);
}
fclose(ptr);
ptr=fopen(“marks.dat”, “r”);
while(feof(ptr)= = 0)
{
fread(&std, sizeof(std), 1, ptr);
printf(“\nRoll Number is %d”, std.rollno);
printf(“\nName is %s”, std.name);
printf(“\nMarks are %d”, std.marks);
}
fclose(ptr);
getch( );
}
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
5
Unit - 5
Question-5
What are the preprocessor directives? Explain with example.
Answer-5
Preprocessor directives are included at the beginning of the program.
Preprocessor directives process source code before compilation. Preprocessor
directives start with # symbol.
1. File inclusion:
There are two file inclusion directives as follows:
1. #include directive
2. #define directive
#include directive:
To include any of the header files you must use the #include directive.
Syntax:
#include<filename>
For example: #include<stdio.h>
#define directive:
To define a preprocessor macro, you must use #define directive.
Syntax:
#define File Name
or,
#define MacroName MacroDefinition
2. Macro substitution:
Macro substitution is a process where an identifier in a program is replaced by
a predefined string or expression. This identifier is known as Macro name.
#define statement is used for this task.
Types of Macro-substitution:
1. Simple macro substitution
2. Macro as arguments
Simple Macro-substitution:
Simple string replacement is commonly used to define constants.
Example:
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
6
Unit - 5
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main( )
{
float a, r;
printf(“Enter radius”);
scanf(“%f”, &r);
a=PI*r*r;
Macro as Arguments:
Macro can be defined with arguments if needed.
Example:
#include<stdio.h>
#include<conio.h>
#define CUBE(x) x*x*x
void main( )
{
int q, n;
printf(“Enter number”);
scanf(“%d”, &n);
q=CUBE(n);
3. Conditional compilation:
Conditional compilation directives are used to conditionally select a part or the
complete program. List of conditional compilation directives are as follows-
1. #if
2. #elif
3. #else
4. #end if
5. #define
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
7
Unit - 5
6. #undef
7. #ifdef
8. #ifndef
9. #error
10. #pragma