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

Dsa Assignment

The document discusses different file types (text and binary), common file operations like creation, opening, reading, writing and closing files. It also provides code snippets to demonstrate how to perform these operations using functions like fopen(), fread(), fwrite(), and fclose(). The document then presents an example of indexed sequential search to quickly search and access records from a large file.

Uploaded by

Harshvi Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Dsa Assignment

The document discusses different file types (text and binary), common file operations like creation, opening, reading, writing and closing files. It also provides code snippets to demonstrate how to perform these operations using functions like fopen(), fread(), fwrite(), and fclose(). The document then presents an example of indexed sequential search to quickly search and access records from a large file.

Uploaded by

Harshvi Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Before moving on to details of files and their operations we shall discuss a bit about the type of

Files that we use here for different operations.

Text and Binary are the file types that you should have a basic idea before moving further into
the topic.

1. TEXT FILES –
A text file contains textual information in the form of alphabets, digits and special characters
or symbols.
This file can be created using a text editor and can be read and understood by a human
being as they are in plain language.

2. BINARY FILES –
On the other hand, a binary file contains bytes or a compiled version of a text file i.e. data
in the form of 0’s and 1’s. They can hold higher amount of data, are not readable easily and
provides a better security than text files.

Different operations that can be performed on a file are:


1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgetc)
4. Writing to a file (fprintf or fputs)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
The text in the brackets denotes the functions used for performing those operations.
Opening or creating file –
For opening a file, fopen function is used with the required access modes. Some of
the commonly used file access modes are:
 “r” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up
a pointer which points to the first character in it. If the file cannot be opened fopen( ) returns
NULL.
 “w” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new
file is created. Returns NULL, if unable to open file.
 “a” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up
a pointer that points to the last character in it. If the file doesn’t exist, a new file is created.
Returns NULL, if unable to open file.
 “r+” – Searches file. If is opened successfully fopen( ) loads it into memory and sets up a
pointer which points to the first character in it. Returns NULL, if unable to open the file.
 “w+” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a
new file is created. Returns NULL, if unable to open file.
 “a+” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets
up a pointer which points to the last character in it. If the file doesn’t exist, a new file is created.
Returns NULL, if unable to open file.
As given above, if you want to perform operations on binary file, then you have to
append ‘b’ at the last. For example, instead of “w” you have to use “wb”, insead of
“a+” you have to use “a+b”. For performig the operations on file, a special pointer
called File pointer is used which is decalared as

FILE *fp;

So, the file can be opened as

fp = fopen(“fileName.txt”, “w”)
The second parameter can be changed to contain all the attributes listed in the
above table.

Reading from a file –


The file read operations can be perfomed using functions fscanf or fgets. Both the
functions performd the same operations as that of printf and gets but with an
additional parameter, the file pointer. So, it depends on you if you want to read the
file line by line or character by character.
And the code snippet for reading a file is as:

FILE * fp;

fp = fopen(“fileName.txt”, “r”);

fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);

Writing a file –:
The file write operations can be perfomed by the functions fprintf and fputs with
similarities to read operations. The snippet for writing to a file is as :

FILE *fp ;

fp = fopen(“fileName.txt”, “w”);

fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);

Closing a file –:
After every successful fie operations, you must always close a file. For closing a file,
you have to use fclose function. The snippet for closing a file is given as :
FILE *fp ;

fp= fopen(“fileName.txt”, “w”);

---------- Some file Operations -------

fclose(fp)

/*Indexed Sequential Search*/


#include <stdio.h>
#include <conio.h>
#define MAX 5
struct mainfile
{ int empid;
char name[25];
float basic;
};
struct indexfile
{
int index_id;
int kindex;
};
int main()
{
struct mainfile fobj[MAX];
struct indexfile index[MAX];
int i, num, low, high, ct = 4;
float basicsal;
for (i = 0;i < MAX;i++)
{
printf("\nEnter employee id?");
scanf("%d", &fobj[i].empid);
printf("\nEnter name?");
scanf("%s", fobj[i].name);
printf("\nEnter basic?");
scanf("%f", &basicsal);
fobj[i].basic = basicsal;
}
printf("\nNow creating index file...!");
for (i = 0;i < (MAX / 5);i++)
{
index[i].index_id = fobj[ct].empid;
index[i].kindex = ct;
ct = ct + 5;
}
printf("\n\nEnter the empid to search?");
scanf("%d", &num);
for (i = 0;(i < MAX / 5) && (index[i].index_id <= num);i++);
low = index[i-1].kindex;
high = index[i].kindex;
for (i = low;i <= high;i++)
{
if (num == fobj[i].empid)
{
printf("\nThe record is: \n\t");
printf("\nEmpid: %d", fobj[i].empid);
printf("\nName: %s", fobj[i].name);
printf("\nBasic: %f", fobj[i].basic);
getch();
return;
}
}
printf("\nNumber not found...!");
return 0;
}

1. Quite easy to process,


   2. With proper selection of a key field, records in a large file can be searched and
accessed in very quickly.
   3. Any field of the records can be used as the key. The key field can be numerical or
alphanumerical. 

You might also like