Derivatives Functions
Derivatives Functions
FILE HANDLING
• PROBLEM:-if we need same data to be processed again
and again, we have to enter it again and again and if volume
of data is large, it will be time consuming process.
• SOLUTION:-data can be permanently stored, a program can
read it directly at a high speed.
• The permanent storage space on a disk is called FILE.
• FILE is a set of records that can be accessed through the set
of library functions.
FILE TYPES
1.SEQUENTIAL FILE:-in this data is kept permanently. If we want
to read the last record of file then we need to read all the
records before that record.it means if we wan to read 10th
record then the first 9 records should be read sequentially for
reaching to the 10th record.
• If you are going to handle binary files then you will use below mentioned access modes
instead of the above mentioned:
• "rb", "wb", "ab", "ab+", "a+b", "wb+", "w+b"
void main()
{
FILE *fp;
fp=fopen(“data.txt”,”r”);
If(fp = = NULL)
{
printf(“cannot open file”);
}
getch();
}
Closing a file
• To reopen it in some other mode
• To prevent any misuse with the file
• To make the operations you have performed
on the files successful.
To close a file, we must use function fclose()
Syntax:
int fclose(FILE *stream);
fclose()
• It closes the named stream. We can pass
pointer variable that points to a file to be
closed. All buffers associated with the stream
are flushed before closing.
• On success, fclose() returns 0 otherwise, it
returns end of function (EOF).
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
clrscr();
fptr=fopen("a.txt",“w");
if(fptr==null)
printf("\n file cannot be opened for creation");
else
printf("\n file created successfully");
fclose(fptr);
}
Binary modes:-
s
Reading from a file
Different functions to read a char are fgetc and getc. Both
perform similar functions and have the same syntax.
ch = fgetc (fp);
fseek(p,10L,0);
origin is 0,means that displacement will be relative to
beginning of file, so position pointer is skipped 10 bytes
forward from beginning of file.2nd argument is long integer so
L is attached with it.
fseek(p,8L,SEEK_SET);
position pointer is skipped 8 bytes forward from beginning of
file.
fseek(p,-6L,SEEK_END);
position pointer is skipped 6 bytes backward from the end of
file.
Replace a char x by char y
main()
{
FILE *fp;
char temp,name[];
printf(“enter name of file”);
scanf(“%s”,name);
fp=fopen(“name”,”r”);
while((temp=getc(fptr))!=EOF)
{
if(temp= =‘x’)
{
fseek(fp, -1,1);
putc(‘y’,fp);
}
}
fclose(fp);
}
ftell():-it is required to find the current location of the file pointer
within the file
ftell(fptr);
Fptr is pointer for currently opened file.
main()
{
FILE *fp;
char ch;
fp=fopen(“text”,”r”);
fseek(fp,241,0);
ch=fgetc(fp);
while(!feof (fp))
{
printf(“%c”,ch);
printf(“%d”,ftell(fp));
ch=fgetc(fp);
}
fclose(fp);
}
Rewind():-it is used to move the file pointer to the beginning of the given
file.
rewind(fptr);
main()
{
FILE *fp;
fp=fopen(“stu.dat”,”r”);
if(fp==NULL)
{
printf(“file can not open”);
exit(0);
}
printf(“position pointer %d”,ftell(fp));
fseek(fp,0,2);
printf(“position pointer %d”,ftell(fp));
rewind(fp);
fclose(fp);
}
• ferror():-it is used for detecting error in the file
when file read write operation is carried out.
It returns the value nonzero if an error occurs
otherwise returns zero value.
ferror(fptr);
WAP that writes the contents entered by user at runtime to a file, then read
the contents of file using function fgetc() and fputc()
void main()
{
FILE *fp;
int c;
clrscr();
fp=fopen(“ABC.doc”,”w”);
if(fp==NULL)
{
printf(“file not opened in write mode”);
getch();
exit(0);
}
printf(“file opened successfully, enter ctrl +z to exit\n”);
while(c=getchar()!=EOF)
{
fputc(c,fp);
}
fclose(fp);
fp=open(“ABC.doc”,”r”);
if(fp==NULL)
{
printf(“file not opened in read mode”);
getch();
exit(0);
}
printf(“file opened for reading”);
c=fgetc(fp);
while(c!=EOF)
{
printf(“%c”,c);
c=fgetc(fp);
}
fclose(fp);
getch();
}
CSE101-lec#12
• Recursive function(or Recursion)
• Mathematical functions in C(<math.h> header
file)
#include <stdio.h>
int addNumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d",addNumbers(num));
return 0;
}
int addNumbers(int n)
{
if(n == 0)
return n;
else
return n + addNumbers(n-1);
}
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
A. 4
B. 8
C. 16
D. 10
A. 13
B. 12
C. 9
D. 10
A. 11001
B. 10011
C. 11111
D. 00000
• This storage class declares register variables which have the same
functionality as that of the auto variables. The only difference is that the
compiler tries to store these variables in the register of the
microprocessor if a free register is available.
• This makes the use of register variables to be much faster than that of the
variables stored in the memory during the runtime of the program. If a
free register is not available, these are then stored in the memory only.
• Usually few variables which are to be accessed very frequently in a
program are declared with the register keyword which improves the
running time of the program. An important and interesting point to be
noted here is that we cannot obtain the address of a register variable
using pointers.
A. 0
B. 1
C. -1
D. Compile time error
• Function name and the number and type of arguments in the function call must
be same as that given in the function declaration and function header of the
function definition
• Names (and not the types) of variables in function declaration, function call and
header of function definition may vary
• Arguments may be passed in the form of expressions to the called function. In
such a case, arguments are first evaluated and converted to the type of formal
parameter and then the body of the function gets executed.
• If the return type of the function is not void, then the value returned by the called
function may be assigned to some variable as given below.
variable_name = function_name(variable1, variable2, …);
Advantages:
• The method doesn't change the original variable, so it is preserving data.
• Whenever a function is called it, never affect the actual contents of the
actual arguments.
• The other advantage could be arguments can be variables(e.g. x),
literals(e.g. 6,0…), or expressions(e.g. x+1)
Disadvantages:
• Copy of the variable is created hence it can consume additional storage
space
• Along with more space, it can take a lot of time to copy, thereby resulting in
performance penalty, especially if the function is called many times
Advantages:
• It provides greater time and space efficiency as duplicate copies of the
arguments are not created
• In this method, there is no copy of the argument made. Therefore it is
processed very fast.
Disadvantages:
• Original values are not safe, as they can be modified
• We can only pass addresses as actual arguments(we cannot pass:
literals(1,2,3…) / or expressions(x+1,y+2..) as actual arguments
Value modification Original value not modified. The original value is modified.