File handling in c language

36
File Handling in ‘C’ Presented By Harish Gyanani

description

File handling in c language

Transcript of File handling in c language

Page 1: File handling in c language

File Handling in ‘C’

Presented By Harish Gyanani

Page 2: File handling in c language

WHAT IS FILE?File is named location of stream of bits.

It may be stored at singe place or different places but it represents a single stream.

Page 3: File handling in c language

What is stream in c programming

language?Stream is not a hardware it is linear queue which connect file to program and passes block of data in both direction .So it is independent of devices which we are using. We can also define stream as source of data. This source can be (a) A file(b) Hard disk or CD, DVD etc.(c) I/O devices etc. In c programming language there are two type of stream. (a) Text streams(b) Binary streams

Page 4: File handling in c language

What is buffer in c programming

language?

Buffer is a technique which reduces number of I/O call.

Page 5: File handling in c language

Create a File#include<stdio.h>main(){

FILE *p;p=fopen(“abcd.txt","w");if(p){

printf("created");}else{

printf("not created");}fclose(p);

}

Page 6: File handling in c language

Write in a Blank File#include<stdio.h>main(){

FILE *p;char a;p=fopen(“file1.txt","w");if(p){

printf("opened\n");fputs("hello i am a student",p);

}else{

printf("not opened\n");}fclose(p);

}

Page 7: File handling in c language

Open a file in default program

//we can open any type of file//Example - ppt,txt,jpg,mp3 etc.#include<stdio.h>main(){

system(“D:\\abcd\\xyz.mp3");}Above code will play xyz.mp3 file in Windows Media Player(default program) located in D drive ->abcd folder

Page 8: File handling in c language

Create, Write and Read A File Altogether.1

//THIS IS A PROGRAM TO DEMONSTRATE CREATE, WRITE AND READ A FILE ALTOGETHER#include<stdio.h>main(){

FILE *y;char t;

 //creatingy=fopen("yashuu.txt","w");if(y){

printf("created\n"); 

}else{

printf("not created\n");}

 //writingfputs("this is yashu",y);fcolse(y);

 

Page 9: File handling in c language

Create, Write and Read A File Altogether.2

//readingy=fopen("yashuu.txt","r");if(y){

printf("opened\n"); 

}else{

printf("not opened\n");}for(;(t=fgetc(y))!=EOF;)

{//fgetc is used to read a character from fileprintf("%c",t);}

fclose(y);}

Page 10: File handling in c language

Count characters in a file

#include<stdio.h>main(){

FILE *y;int count;char ch;fopen("hh.txt","r");for(count=0;(ch=fgetc(y))!=EOF;count++){}

printf("%d",count);fclose(y);

}

Page 11: File handling in c language

Count lines in a File#include<stdio.h>main(){

FILE *y;int count=0;char ch;y=fopen("yy.txt","r");while((ch=fgetc(y))!=EOF){if(ch=='\n'){count++;}}count++;printf("%d",count);fclose(y);

}

Page 12: File handling in c language

Count words of a File

#include<stdio.h>main(){

int count=0;char ch;FILE *y;y=fopen("fight.txt","r");while((ch=fgetc(y))!=EOF){

if(ch==32){

count++;}

 }count++;printf("%d",count);fclose(y);

}

Page 13: File handling in c language

Append a File#include<stdio.h>main(){

FILE *p;char a;p=fopen("yashu65.txt","a");if(p){

printf("opened\n");fputs("hello i m yashu\n",p);

}else{

printf("not opened\n");}fclose(p);

}

Page 14: File handling in c language

Introduction to fseek().1

Sets the position indicator associated with the stream to a new position.

Page 15: File handling in c language

Introduction to fseek().2

Parameters• stream -- This is the pointer to a FILE object that

identifies the stream.• offset -- This is the number of bytes to offset

from whence.• whence -- This is the position from where offset

is added. It is specified by one of the following constants:

Page 16: File handling in c language

Introduction to fseek().3

Constant Description

SEEK_SET Beginning of file

SEEK_CUR Current position of the file pointer

SEEK_END End of file

Page 17: File handling in c language

Introduction to fseek().4

Return ValueThis function returns zero if successful, else it returns nonzero value.

Page 18: File handling in c language

fseek() to read from 6th position

#include <stdio.h>main (){ FILE *fp; char c; fp = fopen("file.txt","r"); fseek( fp, 5, SEEK_SET ); while((c=fgetc(fp))!=EOF) { printf("%c",c); } fclose(fp);}

Page 19: File handling in c language

fseek() example of overwriting

#include <stdio.h>main (){ FILE *fp;  fp = fopen("file.txt","w"); fputs("This is krish", fp);  fseek( fp, 2, SEEK_SET ); fputs("at", fp); fclose(fp);}

Page 20: File handling in c language

fseek() Example of SEEK_SET.1

The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz”

SEEK_SET is used to count characters from the beginning#include<stdio.h>#include<string.h>main(){ FILE *y=fopen("abc.txt","r"); char b; int i; //skipping 5 characters using this loop for(i=0;i<5;i++) { b=fgetc(y); }

Page 21: File handling in c language

fseek() Example of SEEK_SET.2

h

fseek(y,7,SEEK_SET); b=fgetc(y); printf("%c",b); }

The output of this program is –

Because it skips 7 characters from beginning and 8th character of this file is ‘h’

Page 22: File handling in c language

fseek() Example of SEEK_CUR.1

•The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz”

•SEEK_CUR is used to count characters from the currect position of file pointer•#include<stdio.h>•#include<string.h>•main()•{• FILE *y=fopen("abc.txt","r");• char b;• int i;• //skipping 5 characters using this loop• for(i=0;i<5;i++)• {• b=fgetc(y);• }

Page 23: File handling in c language

fseek() Example of SEEK_CUR.2

fseek(y,7,SEEK_CUR); b=fgetc(y); printf("%c",b); }

The output of this program is –

Because it skips 7 characters from current position which is 5; means 5+7 = 12 chars skipped. The 13th character of this file is ‘m’.

m

Page 24: File handling in c language

fseek() Example of SEEK_END

The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz”

SEEK_END set file pointer to the last character position#include<stdio.h>#include<string.h>main(){ FILE *y=fopen("abc.txt","r"); char b; fseek(y,-10,SEEK_END); b=fgetc(y); printf("%c",b); }

The output of this program is – q

-10 means 10th positioned character from the last which is ‘q’.

Page 25: File handling in c language

Example of ftell() #include <stdio.h>main (){ FILE *fp; char c; fp = fopen("file.txt","r"); fseek( fp, 5, SEEK_SET ); while((c=fgetc(fp))!=EOF) { printf("%c - position : %d\n",c,ftell(fp)); } fclose(fp);}

Page 26: File handling in c language

a+ mode

• FILE *f1=fopen("test.dat","a+");• Open for reading and appending (writing at end

of file). The file is created if it does not exist. • The initial file position for reading is at the

beginning of the file, but output is always appended to the end of the file.

• There is just one pointer which initially is at the start of the file but when a write operation is attempted it is moved to the end of the file.

Page 27: File handling in c language

r+ mode

• read/update: Open a file for update (both for input and output). The file must exist.

Page 28: File handling in c language

w+ mode• write/update: Create an empty file and open it

for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

Page 29: File handling in c language

What is difference between file

opening mode r+ and w+?Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of file as well it doesn’t create a new file if such file doesn’t exits while in w+ truncate the content of file as well as create a new file if such file doesn’t exists.

Page 30: File handling in c language

Read integers From File

#include<stdio.h>main(){

FILE *p;int sum=0,number;p=fopen("kl.txt","r");if(p==NULL){printf("file not opened ");}

else{while(!feof(p)){ fscanf(p,"%d",&number); printf("%d\n",number); sum=sum+number;}printf("%d",sum);fclose(p);}

}

Page 31: File handling in c language

Rename a File#include <stdio.h>main(){

int result; char oldname[] ="rt.txt"; char newname[] ="network.txt"; result= rename( oldname , newname ); if ( result == 0 )

{ puts ( "File successfully renamed" ); }

else{

perror( "Error renaming file" );}

}

Page 32: File handling in c language

Delete a File#include <stdio.h>main (){ if( remove( "debug.log" ) != 0 ) { perror( "Error deleting file" ); } else { puts( "File successfully deleted" ); }}

Page 33: File handling in c language

Make a Folder/Directory

#include<stdio.h>main(){

//when folder is created it returns false, otherwise trueint a=system("mkdir d:\\vikas\\abcd");if(a){

printf("folder not created");}else{

printf("folder created");}

}

Page 34: File handling in c language

Rename a Folder#include <stdio.h>main(){ int result; char oldname[] ="faltu"; char newname[] ="faltu2"; result= rename( oldname , newname ); if ( result == 0 ) { puts ( "Folder successfully renamed" ); } else { perror( "Error renaming Folder" ); }}

Page 35: File handling in c language

Scan all files and folders inside a Directory/Folder

#include<stdio.h>main(){

system("dir d:\\fca\\");}

Page 36: File handling in c language

Delete a Folder#include <stdio.h>main (){ if( system("rmdir rt") ) { perror( "Error deleting folder" ); } else { puts( "Folder successfully deleted" ); }}