Pps Unit III
Pps Unit III
I B Tech CSE
Unit-III
B Pravalika
Assistant Professor
Department of Computer Science & Engineering
Bhoj Reddy Engineering College for Women
Unit-III 1
Unit-III 2
Topics:
Introduction to Files
A buffer is a temporary storage area that holds data while they are being
transferred to or from memory.
Unit-III 4
Text vs Binary Files:
Text Files
1. All Data are human readable text characters
2. Each line ends with a newline character
3. EOF character marker at the end of the file
Binary Files
1. All Data are in the same format they are stored in.
2. There are no line no newline character
3. EOF marker
Unit-III 5
Working with Files:
Unit-III 6
Streams:
C declares and defines three stream pointers in the stdio.h header file.
stdin-points to standard input stream
stdout-points to standard output stream
stderr-points to standard input stream
Unit-III 7
Standard I/O functions:
Unit-III 8
Standard I/O functions:
Unit-III 9
File Open/Close:
fopen(“filename”, “mode”)
fclose(stream)
Text Files:
r, w, a, r+, w+, a+
Binary Files:
rb, wb, ab, rb+, r+b, wb+, w+b, ab+, a+b
Unit-III 10
Standard I/O functions:
Unit-III 11
Formatted Input/Output:
Unit-III 12
Formatted Input/Output: Program-1
#include <stdio.h>
int main()
{
int a,b;
fprintf(stdout,"\nEnter a value:");
Output:
fscanf(stdin,"%d",&a);
Enter a value:25
fprintf(stdout,"val=%d",a); val=25
Enter b value:35
fprintf(stdout,"\nEnter b value:"); val=35
scanf ("%d",&b);
printf("val=%d",b);
}
Unit-III 13
Formatted Input/Output: Program-2
#include<stdio.h>
int main( )
{
FILE *fp1;
fp1=fopen("First.txt","w");
fclose(fp1);
}
Unit-III 14
Standard I/O functions:
Unit-III 15
Character Input/Output:
Character
I/O
Terminal Any
Only Stream
Unit-III 16
Character Input/Output:
int getchar( )
int putchar(int c)
Unit-III 17
Character Input/Output: Program-3
#include<stdio.h>
int main()
{
char x;
FILE *fp1;
fp1=fopen("First.txt","r");
x=fgetc(fp1);
fputc(x,stdout);
x=fgetc(fp1);
putchar(x);
Output:
fclose(fp1);
} He
Unit-III 18
Character Input/Output: Program-4
#include<stdio.h>
int main()
{
char x='J',y='R';
FILE *fp1;
fp1=fopen("Second.txt","w");
fputc(x,fp1);
fputc(y,fp1);
fclose(fp1);
}
Unit-III 19
Character Input/Output: Program-5
#include<stdio.h>
int main( )
{
char x='P',y='Q';
FILE *fp1;
fp1=fopen("First.txt","a");
fputc(x,fp1);
fputc(y,fp1);
fclose(fp1);
}
Unit-III 20
Character Input/Output: Display Contents of a File
#include<stdio.h>
int main( )
{
char x;
FILE *fp1;
fp1=fopen("One.txt","r");
if(fp1==NULL)
printf("\nFile doesnot exist");
else
{
x=fgetc(fp1); Output:
while(x!=EOF)
{ We are working with File Input and Output
fputc(x,stdout);
x=fgetc(fp1);
}
}
fclose(fp1);
} Unit-III 21
Character Input/Output: Copy one File to another
int main()
{
char x;
FILE *fp1,*fp2;
fp1=fopen("First.txt","r");
fp2=fopen("Second.txt","w");
if(fp1==NULL)
printf("\nFile doesnt exist");
else
{
x=fgetc(fp1);
while(x!=EOF)
Output:
{ Contents of file copied
fputc(x,fp2); successful
x=fgetc(fp1);
}
printf("Contents of file copied successful");
}
fclose(fp1);
fclose(fp2);
} Unit-III 22
Character Input/Output: Merge two Files
else
{
x=fgetc(fp1);
while(x!=EOF)
{
#include<stdio.h> fputc(x,fp3);
int main( ) x=fgetc(fp1);
{ }
char x; x=fgetc(fp2);
FILE *fp1,*fp2,*fp3; while(x!=EOF)
fp1=fopen("First.txt","r"); {
fp2=fopen("Second.txt","r"); fputc(x,fp3);
fp3=fopen("Third.txt","w"); x=fgetc(fp2);
if(fp1==NULL || fp2==NULL) }
{ printf("Contents of both the files
printf("\nFile doesnt exist");
} copied
successful");
}
fclose(fp1);
fclose(fp2);
Unit-III 23
fclose(fp3);
}
Character Input/Output:
Output:
Unit-III 24
Standard I/O functions:
Unit-III 25
Line Input/Output:
fgets reads a line from the specified stream and stores it into the string
pointed to by str. It stops when either (size-1) characters are read or the
newline character is read, or the end-of-file is reached, whichever comes
first.
Unit-III 26
Standard I/O functions:
Unit-III 27
Binary files Block Input/Output Functions:
Unit-III 28
Binary files Block Input/Output Functions:
#include <stdio.h>
int main( )
{
int a[5]={10,20,30,40,50};
FILE *fptr;
fptr = fopen(“Fourth.bin","wb");
fclose(fptr);
}
Unit-III 29
Binary files Block Input/Output Functions:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int b[20],i; Output:
FILE *fptr;
10
fptr = fopen("Fourth.bin","rb"); 20
30
fread(b, sizeof(int), 5, fptr); 40
50
for(i = 0; i < 5; i++)
{
printf("%d\n",b[i]);
}
fclose(fptr);
return 0;
} Unit-III 30
Standard I/O functions:
Unit-III 31
Positioning functions
Unit-III 32
Unit-III 33
Standard I/O functions:
Unit-III 34
System file operations:
Unit-III 35
Standard I/O functions:
Unit-III 36
File status functions:
• feof() returns non-zero if the file position indicator is at the end of the file,
otherwise it returns zero.
• The ferror() function is used to check for the file error on given stream. A
return value of zero indicates no error has occurred, whereas a non-zero
value indicates an error occurred.
Unit-III 37
Preprocessors Commands:
C preprocessor modifies a source file before handing it over to the
compiler.
Tasks of a preprocessor:
1. File inclusion
2. Macro definition
3. Conditional compilation
4. Line
5. Error
6. Pragma
Unit-III 38
Preprocessors Commands: File inclusion command
#include <stdio.h>
#include "myheader.h"
Unit-III 39
Preprocessor Commands: Example
int fact(int a)
{
int i,f;
f=1;
for(i=a;i>=1;i--)
f=f*i;
return f; my.h header file
}
int gcd(int a, int b)
{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
} Unit-III 40
Preprocessor Commands: Example
#include<stdio.h>
#include "my.h"
int main( )
{ Output:
int num;
num=fact(5); Factorial is :120
printf("\nFactorial is :%d",num); GCD is :36
num=gcd(180, 108);
printf("\nGCD is :%d",num);
}
Unit-III 41
Preprocessors Commands: Macro definition
The macro definition command associates a name, referred as macro name
and a sequence of tokens, referred as macro body.
The macro body is the text that specifies how the macro name is replaced in
the program.
#include<stdio.h>
#define N 20
#define sum(x) x+2
int main( ) Output:
{
int num; Value is:20
num=N; Value is:7
printf("\n Value is:%d",num);
num=sum(5);
printf("\n Value is:%d",num);
}
Unit-III 43
Preprocessors Commands: Example2
#include<stdio.h>
#define N 20 num=10/sum(5);
#define sum(x) x+2
int main( ) num=10/5+2;
{ Output:
int num;
num=N; Value is:20
printf("\n Value is:%d",num); Value is:7
num=sum(5); Value is:4
printf("\n Value is:%d",num);
num=10/sum(5);
printf("\n Value is:%d",num);
}
Unit-III 44
Preprocessors Commands: Example3
#include<stdio.h>
#define N 20 num=10/sum(5);
#define sum(x) (x+2)
int main( ) num=10/(5+2);
{ Output:
int num;
num=N; Value is:20
printf("\n Value is:%d",num); Value is:7
num=sum(5); Value is:1
printf("\n Value is:%d",num);
num=10/sum(5);
printf("\n Value is:%d",num);
}
Unit-III 45
Preprocessors Commands: Example4
#include<stdio.h>
#define N 20
#define sum(x) x+2
#define add(y) sum(y)+2
int main( ) Output:
{
int num; Value is:20
num=N; Value is:7
printf("\n Value is:%d",num); Value is:9
num=sum(5);
printf("\n Value is:%d",num);
num=add(5);
printf("\n Value is:%d",num);
}
Unit-III 46
Preprocessors Commands: Macro definition
Predefined macros
__DATE__
__FILE__
__LINE__
__TIME__
__STDC__
Unit-III 47
Preprocessors Commands: Example5
#include<stdio.h>
int main( )
{
int num;
printf("\nWelcome"); Output:
printf("\n%s",__DATE__);
printf("\n%s",__FILE__); Welcome
printf("\n%d",__LINE__); Mar 15 2021
printf("\n%s",__TIME__); D:\Programs\PP01.c
} 8
14:51:43
Unit-III 48
Preprocessor Commands: Example
#define N 10
int fact(int a)
{
int i,f;
f=1;
for(i=a;i>=1;i--)
f=f*i;
my.h header file
return f;
}
int gcd(int a, int b)
{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
} Unit-III 49
Preprocessor Commands: Example
#include<stdio.h>
#include "my.h"
int main() Output:
{
int num=N; Value is :10
printf("\nValue is :%d",num);
}
Unit-III 50
Preprocessor Commands: Example
#include<stdio.h>
#include "my.h"
#define N 20
#define sum1(a,b) a+b
#define sum2(a,b) (a+b)
ERROR
int main()
{
int num=N;
printf("\nValue is :%d",num);
num=10/sum1(2,4);
printf("\nValue is :%d",num);
num=10/sum2(2,4);
printf("\nValue is :%d",num);
}
Unit-III 51
Preprocessor Commands: Example
#include<stdio.h>
#include "my.h"
#undef N
#define N 20
#define sum1(a,b) a+b
#define sum2(a,b) (a+b) Output:
int main( )
{ Value is :20
int num=N; Value is :9
printf("\nValue is :%d",num); Value is :1
num=10/sum1(2,4);
printf("\nValue is :%d",num);
num=10/sum2(2,4);
printf("\nValue is :%d",num);
}
Unit-III 52
Preprocessor Commands: Conditional Compilation
#if
#else
#endif
#elif
#ifdef
#ifndef
Unit-III 53
Preprocessor Commands: Example
#include<stdio.h>
#include "my.h"
#undef N
#define N 20 Output:
#if N
#define M 30 Value is :30
#endif
int main()
{
int num=M;
printf("\nValue is :%d",num);
}
Unit-III 54
Preprocessor Commands: Example
#include<stdio.h>
#include<conio.h>
#include "my.h"
#ifdef N
#undef N
#define N 20
#else
#define N 20
#endif
int main()
{
int num=N;
printf("\nValue is :%d",num);
}
Unit-III 55
Preprocessor Commands: Line
#line 100
The #line is a preprocessor directive used to reset the line number in the
code.
Unit-III 56
Preprocessor Line:
#include<stdio.h>
int main( )
{
int num;
printf("\nWelcome"); Output:
printf("\nHello World");
printf("\n%d",__LINE__); Welcome
printf("\nHai"); Hello World
#line 52 7
printf("\nHello"); Hai
printf("\n%d",__LINE__); Hello
} 53
Unit-III 57
Preprocessor Commands: Error
#error Welcome
#include<stdio.h>
#include<conio.h>
#include "my.h"
#ifdef N
#undef N
#define N 20
#else
#error Hello
#endif
int main()
{
int num=N;
printf("\nValue is :%d",num);
}
Unit-III 58
Thank You
Unit-III 59