+ All Categories
Home > Documents > File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a...

File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a...

Date post: 11-Jul-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
20
File Handling in C File Handling in C
Transcript
Page 1: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

File Handling in CFile Handling in C

Page 2: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

The slides here are not The slides here are not comprehensive. And covers comprehensive. And covers some part of the lecturesome part of the lectureFile Reading list…File Reading list…File Reading list…File Reading list…Read the following pages from Read the following pages from Herbert Herbert SheildSheild Teach yourself Teach yourself book:book:Pages:Pages: 257 to 297257 to 297

Page 3: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

What is a File?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 storage so that the contents of files remain intact when a computer shuts down.

When a computer reads a file, it copies the file When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.

C uses a structure called FILEFILE (defined in stdio.hstdio.h) to store the attributes of a file.

Page 4: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

Steps in Processing a FileSteps in Processing a File

1. Create the stream via a pointer variable using the FILEFILE structure:FILE *p;FILE *p;

2. Open the file, associating the stream name with the file name.name with the file name.

3. Read or write the data.

4. Close the file.

Page 5: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

The basic file operations areThe basic file operations are

fopen - open a file- specify how its opened (read/write) and type (binary/text)

fclose - close an opened file fread - read from a file

fwrite - write to a file fread - read from a file

fwrite - write to a file fseek/fsetpos - move a file pointer to

somewhere in a file. ftell/fgetpos - tell you where the file pointer is

located.

Page 6: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

File Open ModesFile Open Modes

from Table 7-1 in Forouzan & Gilberg, p. 400

Page 7: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

More on File Open ModesMore on File Open Modes

from Figure 7-4 in Forouzan & Gilberg, p. 401

Page 8: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

Additionally,Additionally,

r+ - open for reading and writing, start at beginning

w+ - open for reading and writing (overwrite file)(overwrite file)

a+ - open for reading and writing (append if file exists)

Page 9: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

File OpenFile Open

The file open function (fopenfopen) serves two purposes:◦ It makes the connection between the physical

file and the stream.

◦ It creates “a program file structure to store ◦ It creates “a program file structure to store the information” C needs to process the file.

Syntax:filepointer=filepointer=fopen(“filename”, fopen(“filename”, “mode”);“mode”);

Page 10: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

More On More On fopenfopen

The file mode tells C how the program will use the file.

The filename indicates the system name and location for the file.and location for the file.

We assign the return value of fopenfopen to our pointer variable:spDataspData = = fopenfopen(“MYFILE.TXT”, “w”);(“MYFILE.TXT”, “w”);

spDataspData = = fopenfopen(“d:(“d:\\\\MYFILE.TXT”, “w”);MYFILE.TXT”, “w”);

Page 11: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

More On More On fopenfopen

from Figure 7-3 in Forouzan & Gilberg, p. 399

Page 12: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

Closing a FileClosing a File

When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file.same file.

To close a file, we use fclosefclose and the

pointer variable:fclose(spData);fclose(spData);

Page 13: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

Syntax:Syntax:fprintf (fp,"string",variables);

Example:Example:int i = 12; float x = 2.356;

fprintffprintf()()

int i = 12; float x = 2.356; char ch = 's'; FILE *fp;fp=fopen(“out.txt”,”w”);fprintf (fp, "%d %f %c", i, x, ch); fclose(fp);

Page 14: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

fscanffscanf()()

Syntax:Syntax:

fscanf (fp,"string",identifiers);

Example:Example:FILE *fp;FILE *fp;

fp=fopen(“input.txt”,”r”);

int i;

fscanf (fp,“%d", &i);

printf(“%d”, i);

fclose(fp);

Page 15: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

fgetcfgetc()()

Syntax:Syntax:identifier = fgetc (file pointer);Example:Example:FILE *fp; fp=fopen(“input.txt”,”r”);FILE *fp; fp=fopen(“input.txt”,”r”);char ch; ch = fgetc (fp); printf(“%c”,ch);fclose(fp);

Page 16: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

write a single character to the output file, pointed to by fp.

Example:Example:

FILE *fp;

fputcfputc()()

FILE *fp;

char ch;

fp=fopen(“input.txt”,”r”);

putc (ch, fp); fclose(fp);

Page 17: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

End of FileEnd of File

There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the fscanffscanf function:

FILE *fptr;int istatus ;fptr=fopen(“input.txt”,”r”);istatus = fscanf (fptr, "%d", &var) ;fptr=fopen(“input.txt”,”r”);istatus = fscanf (fptr, "%d", &var) ;if ( istatus == feof(fptr1) ){

printf ("End-of-file encountered.\n”) ;}

Page 18: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

Reading and Writing FilesReading and Writing Files#include <stdio.h>int main ( ){

FILE *outfile, *infile ;int b = 5, f ;float a = 13.72, c = 6.68, e, g ;

outfile = fopen ("testdata", "w") ;outfile = fopen ("testdata", "w") ;fprintf (outfile, “ %f %d %f ", a, b, c) ;fclose (outfile) ;infile = fopen ("testdata", "r") ;fscanf (infile,"%f %d %f", &e, &f, &g) ;printf (“ %f %d %f \n ", a, b, c) ;printf (“ %f %d %f \n ", e, f, g) ;fclose (infile) ;

}

Page 19: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

ExampleExample#include <stdio.h>

#include<conio.h>

void main()

{

char ch;

FILE *fp;

fp=fopen("out.txt","r");fp=fopen("out.txt","r");

while(2==2){ch=getc(fp);if(feof(fp))

break;printf("\n%c",ch);

}

getch();

}

Page 20: File Handling in Cashikur.buet.ac.bd/CSE101/File.pdf · The basic file operations are fopen -open a file-specify how its opened (read/write) and type (binary/text) fclose -close an

THANK YOUTHANK YOU


Recommended