0% found this document useful (0 votes)
16 views59 pages

Pps Unit III

The document covers Unit-III of a programming course focused on file handling in C, detailing the differences between text and binary files, file operations, and standard I/O functions. It includes practical examples of creating, reading, and writing files, as well as using various file manipulation functions. Additionally, it discusses preprocessor commands and macro definitions in C programming.

Uploaded by

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

Pps Unit III

The document covers Unit-III of a programming course focused on file handling in C, detailing the differences between text and binary files, file operations, and standard I/O functions. It includes practical examples of creating, reading, and writing files, as well as using various file manipulation functions. Additionally, it discusses preprocessor commands and macro definitions in C programming.

Uploaded by

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

Programming for Problem Solving

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

 Text and Binary files

 Creating, Reading and writing text files

 Creating, Reading and writing binary files

 Appending data to existing files

 Writing and reading structures using binary files

 Random access using fseek, ftell and rewind functions.


Unit-III 3
File:

A file is an external collection of related data treated as a unit.

A file is a collection of data stored in one unit, identified by a filename. It


can be a document, picture, audio or video stream, data library,
application, or other collection of data.

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:

There are four steps to work with a file:

1. Create the stream.

2. Open the file.

3. Process the file (read,write).

4. Close the file.

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

File streams are created as a pointer of FILE

Unit-III 7
Standard I/O functions:

The stdio.h header file contains several different input/output functions.


They are grouped into eight categories.
1. File Open/Close
2. Formatted Input/Output
3. Character Input/Output
4. Line Input/Output
5. Block Input/Output
6. File Positioning
7. System File Operations
8. File Status

Unit-III 8
Standard I/O functions:

Unit-III 9
File Open/Close:

fopen(“filename”, “mode”)
fclose(stream)

C has different modes.

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:

scanf(“control string”, addresslist);


printf (“control string”, valuelist);

fscanf (streampointer, “control string”, addresslist);


fprintf (streampointer, “control string”, valuelist);

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

fprintf(fp1,"Help the poor");

fclose(fp1);
}

Unit-III 14
Standard I/O functions:

Unit-III 15
Character Input/Output:

Character
I/O

Terminal Any
Only Stream

Input Output Input Output Push


Back
getchar putchar getc/fgetc putc/fputc ungetc

Unit-III 16
Character Input/Output:

int getchar( )
int putchar(int c)

int getc(FILE *fp)


int putc(int c, FILE *fp)

int fgetc(FILE *fp)


int fputc(int c, FILE *fp)

int ungetc(int c, FILE *fp)

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:

Contents of both the files copied


successful

Unit-III 24
Standard I/O functions:

Unit-III 25
Line Input/Output:

char* gets(char* strptr)


char* fgets(char* strptr, int size, FILE *sp)

int puts(const char* strptr)

int fputs(const char* strptr,FILE *sp)

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:

int fread(void* pInArea, int elementsize, int count, FILE* sp)

int fwrite(void* pOutArea, int elementsize, int count, FILE* sp)

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

fwrite(a, sizeof(int), 5, fptr);

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

void rewind(FILE* sp)


long int ftell(FILE* sp)
int fseek(FILE* sp, long offset, int wherefrom)

Some constants are defined for “wherefrom”

SEEK_SET Starts the offset from the beginning of the file.

SEEK_END Starts the offset from the end of the file.

Starts the offset from the current location of


SEEK_CUR
the cursor in the file.

Unit-III 32
Unit-III 33
Standard I/O functions:

Unit-III 34
System file operations:

int remove(char* filename)


int rename(const char* oldname, const char* newname)
FILE* tmpfile(void)

Unit-III 35
Standard I/O functions:

Unit-III 36
File status functions:

int feof(FILE* sp)


int ferror(FILE* sp)
void clearerr(FILE* sp)

• 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.

The preprocessor uses programmer supplied commands to prepare the


source program for compilation.

All preprocessor commands begin with a pound symbol (#).

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

The preprocessor command is #include and has two formats:

#include <stdio.h>

#include "myheader.h"

The first format to include header files from system library

The second format to include header files from local directory

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.

#define name body

The macro body is the text that specifies how the macro name is replaced in
the program.

Macros must be coded only in a single line.

To continue in next line use backslash ( \ )

A macro definition can simulate a constant definition.

A macro definition can simulate a function definition (with and without


parameters).

We can have nested macros


Unit-III 42
Preprocessors Commands: Example1

#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.

We can reset line number from any line 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

You might also like