0% found this document useful (0 votes)
46 views

Computer Science Unit-5 Sem 1

The document discusses file operations in C language. It explains different types of files like text files, variable record length files and fixed record length files. It provides programs to write and read from text and data files. It also explains preprocessor directives in C with examples.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Computer Science Unit-5 Sem 1

The document discusses file operations in C language. It explains different types of files like text files, variable record length files and fixed record length files. It provides programs to write and read from text and data files. It also explains preprocessor directives in C with examples.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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-

Bell Infotech System is a software


development organization. It is
committed to deliver quality products.

Variable Record Length files:


Variable record length files can contain numbers, characters, and strings also.
For example:

Name Basic Pay DA HRA


John 5000 3000.50 600
Smith 7000 5500.70 800
Ram 4000 5500 700

Fixed Record Length files:


In Fixed record length files, tha data is stored as blocks of fixed number of
bytes.

For example: if Name occupies 30 bytes, Basic Pay occupies 2 Bytes, DA


occupies 4 bytes and HRA occupies 2 bytes (i.e. total=38 bytes) then all the
records will occupies 38 bytes.

There are basically two types of File operations:


1. Writing in the File
2. Reading from the File

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
2
Unit - 5
Question-2
Write a program to write some text in a Text file and also read that text.

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);

fprintf(ptr, “%s\n”, name);


fprintf(ptr, “%d\n”, basic);
fprintf(ptr, “%f\n”, da);
fprintf(ptr, “%d\n”, 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);

printf(“\nName is %s”, name);


printf(“\nBasic pay is %d”, basic);
printf(“\nDA is %f”, da);
printf(“\nHRA is %d”, hra);
}
fclose(ptr);
getch( );
}

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida
4
Unit - 5
Question-4
Write a program that accepts Roll number, Name, and Marks of some students,
write it in a file(fixed record length file) and after that also print all the records.
Answer-4
#include<stdio.h>
#include<conio.h>
struct Student
{
int rollno;
char name[30];
int marks;
}std ;

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.

There are three types of preprocessor directives.


1. File inclusion
2. Macro Substitution
3. Conditional compilation directives

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

For example: #define “Test.c”


Or,
#define MAX 10

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;

printf(“Area= %f”, a);


getch( );
}

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);

printf(“Cube= %d”, q);


getch( );
}

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

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S Engineering College, Greater noida

You might also like