file handling in c language

Post on 07-May-2015

1.407 Views

Category:

Education

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

File handling in c language

TRANSCRIPT

File Handling in ‘C’

Presented By Harish Gyanani

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.

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

What is buffer in c programming

language?

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

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

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

printf("created");}else{

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

}

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

}

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

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

 

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

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

}

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

}

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

}

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

}

Introduction to fseek().1

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

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:

Introduction to fseek().3

Constant Description

SEEK_SET Beginning of file

SEEK_CUR Current position of the file pointer

SEEK_END End of file

Introduction to fseek().4

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

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

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

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

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’

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);• }

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

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

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

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.

r+ mode

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

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.

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.

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

}

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

}

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

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

}

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

Scan all files and folders inside a Directory/Folder

#include<stdio.h>main(){

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

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

top related