FILE HANDLING.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Files in C Rohit Khokher.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
File I/O.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 4 File Processing
RECURSION Recursion is a process where a function calls itself. main()
C Programming Files I/O
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
EKT120: Computer Programming
File I/O.
File Handling in C.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
What you need for the 1st phase of project
File Handling in C.
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
CSE1320 Strings Dr. Sajib Datta
CSC215 Lecture Input and Output.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
File Handling in C.
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
File Handling in C.
Chapter 12: File I/O.
FILE handeling.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

FILE HANDLING

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. 2.RANDOM ACCESS FILE:- in this data can be read and modified randomly.if we want to read the last record of the file,we can read it directly.it takes less time as compared as sequential file.

HOW TO DEFINE A FILE Before using a file in a program,we must open it,which establishes a link between the program and the operating system.A file must be defined as:- FILE *fp; data structure file pointer where FILE is the data structure which is included in the header file,STDIO.H and fp is declared as a pointer which contains the address of a character which is to be read.

Operation on a file FILE *fopen(const char *path, const char *mode); fp = fopen (“ABC.DOC”, “r”); Here fp =file pointer fopen= function to open a file ABC.DOC=name of the file r=mode in which file is to be opened fopen is a function which contains two arguments:- File name ABC.DOC Mode in which we want to open the file. Both these are of string types.

fopen():- This function loads the file from disk to memory and stores the address of the first character to be read in the pointer variable fp. If file is absent,it returns a NULL pointer variable. fclose(fp):- the file which is opened need to be closed and only can be done by library function fclose. int fclose(FILE *fp);

File opening modes Read mode(r):- purpose to read from the file.if file exists,it sets up a pointer which points to first char in it and if it does not exist,it returns NULL. Write mode(w):-purpose to write in the file.if file exists,its contents are overwritten.if it does not exist new file is created. Append mode(a):-purpose is to add data in already existing file.if it does not exist new file is created.

4)Read +(r +):-it is an extension to read mode 4)Read +(r +):-it is an extension to read mode.the operations possible on this mode are to read existing,writing new contents and modifying existings contents.it returns NULL if file does not exist. 5)Write +(w +):-it is similar to write mode, mode.the operations possible on this mode are to write new contents and modify contents already written. 6)Append +(a +):- it is similar to append mode,the operations possible on this mode are to read existing contents,add new contents at the end of file but cannot modify existing contents.

main() { FILE *fp; fp=fopen(“data.txt”,”r”); If(fp = = NULL) printf(“cannot open file”); } exit(0);

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

wb(write):-it opens a binary file in write mode. Binary modes:- wb(write):-it opens a binary file in write mode. fp=fopen(“data.dat”,”wb”); Here data.dat file is opened in binary mode for writing. rb(read):-it opens a binary file in read mode. fp=fopen(“data.dat”,”rb”); Here data.dat file is opened in binary mode for reading.

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); char type function file pointer variable name fgets() used to read strings from a file and copy string to a memory location which is referenced by array. fgets(str, n, fptr); array of chars max.length file pointer

#include<stdio.h> #include<conio.h> void main() { FILE *ptr; char ch; char ch1; ptr=fopen("char.txt","r"); ch=getc(ptr); ch1=fgetc(ptr); fclose(ptr); printf("%c\n\n%c\n\n",ch,ch1); }

Writing chars or char array to a file fputc() or putc():-both these function write the character in the file at the position specified by pointer. putc (c, fp); function character file pointer type variable The file pointer automatically moves 1 position forward after printing characters in the file. fputs()- function write the character array or string in the file at the position specified by pointer. fputs(str, fptr);

#include<stdio.h> #include<conio.h> void main() { FILE *ptr; char ch='A'; char ch1='Z'; ptr=fopen("char.txt","w"); putc(ch,ptr); fputc(ch1,ptr); fclose(ptr); }

#include<stdio.h> #include<conio.h> void main() { FILE *ptr; char str[]="Hello World"; char str1[]="Bye Bye."; ptr=fopen("string.txt","w"); fputs(str,ptr); fputs(str1,ptr); fclose(ptr); }

#include<stdio.h> #include<conio.h> void main() { FILE *ptr; char str[20]; char str1[20]; memset(str,0,20); memset(str1,0,20); ptr=fopen("string.txt","r"); fgets(str1,4,ptr); fclose(ptr); printf("%s\n\n",str1); }

Read &write char into file and prints them #include<conio.h> void main() { FILE *fptr; char c; clrscr(); fptr=fopen("a.txt","r"); printf("the line of text is"); while((c=getc(fptr))!=EOF) printf("%c",c); } fclose(fptr); getch(); #include<conio.h> #include<stdio.h> void main() { FILE *fptr; char c; clrscr(); fptr=fopen("a.txt","w"); printf("Enter the line of text,press ^z to stop "); while(c=getchar()!=EOF) putc(c,fptr); } fclose(fptr);

Program to understand use of fgetc() main( ) { FILE *fptr; char ch; fptr= fopen(“rec.dat”,”r”); if(fptr = = NULL) printf(“file doesn’t exist”); else while( ch = fgetc(fptr) !=EOF) printf(“%c”,ch); } fclose(fptr); EOF:-END OF FILE EOF is an integer value sent to prog.by OS.its value is predefined to be -1 in STDIO.H, No char with the same value is stored on disk.while creatig file,OS detects that last character to file has been sent,it transmits EOF signal.

Writing to file using fputs(sptr,fptr) main() { FILE *fp; char name[20],arr[50]; printf(“enter the file name”); scanf(“%s”,name); fp=fopen(name,”w”); if(fp = = NULL) printf(“file can’t be opened”); exit(0); } else { printf(“the string is “); gets(arr); fputs(arr,fp); } fclose(fp);

fscanf( ):- it is used to read to any type of variable i. e fscanf( ):- it is used to read to any type of variable i.e.,char,int,float,string fscanf(fp,”%c”,&ch); file pointer control char type string variable fscanf(fp,”%c%d%f”,&a,&b,&c); We write stdin,this func call will work similar simple scanf Because stdin means standard i/p i.e.keyboard. fscanf(stdin,”%c”,&ch); Is equivalent to scanf(“%c”,&ch);

#include<conio.h> void main() { FILE *fptr; char name[10]; int sal; fptr=fopen(“rec.dat”,”r”); fscanf(fptr,”%s%d”,name,&sal); while(!feof(fptr)) printf(“%s%d”,name,sal); } fclose(fptr); getch();

fprintf():- it is used to print chars,numbers or strings in a file. fprintf (fp,“%d”,a); With this function,we can print a no. of variables with single call. fprintf (fp,”%c %d %d”,a,b,c); We write stdout,this func call will work similar simple printf Because stdout means standard o/p i.e.keyboard. fprintf(stdout,”%c”,ch); Is equivalent to printf(“%c”,ch);

Program for fprintf() main() { FILE *fp; char name[10]; fp=fopen(“rec.dat”,”w”); printf(“enter your name”); scanf(“%s”,name); fprintf(fptr,”%s”,name); fclose(fp); }

fread():-reads a block(array or structure)from a file. fread( ptr, m, n ,fptr); address of size of no.of arrays file pointer array/structure array/structure fwrite():-writes a block(array or structure)from a file. fwrite( ptr, m, n ,fptr);

Prog for fread() #include<conio.h> struct student { int rollno; char name[20]; }; void main() struct student st; file *fptr; fptr=fopen("d.txt","r"); clrscr(); fread(&st,sizeof(st),1,fptr); printf("roll number is %d",st.rollno); printf("name is %s",st.name); fclose(fptr); getch(); }

Prog for fwrite() #include<conio.h> struct student { int rollno; char name[20]; }; void main() struct student st; file *fptr; fptr=fopen("d.txt","w"); clrscr(); printf("enter roll number"); scanf("%d",&st.number); printf("enter name"); gets(st.name); fwrite(&st,sizeof(st),1,fptr); fclose(fptr); getch(); }

getw():-it is an integer oriented function getw():-it is an integer oriented function.it is used to read an integer from file in which numbers are stored. a=getw(fptr); int type var fun name int type var putw():-it prints a number in the file. putw( a, fptr); lib fun int type var file pointer

Prog for getw() #include<conio.h> void main() { file *fptr; int i,n; clrscr(); fptr=fopen("b.txt","r"); printf("\n the number are") for(i=1;i<=10;i++) n=getw(fptr); printf("%d\t",n); } fclose(fptr); getch();

Prog for putw() #include<conio.h> void main() { file *fptr; int i,n; clrscr(); fptr=fopen("b.txt","w"); for(i=1;i<=10;i++) printf("enter number"); scanf("%d",&n); putw(n,fptr); } fclose(fptr); getch();