File Handling in Programming - Programming - Lecture Slides, Slides of C programming

This lecture is about File handling n C language. Codes about opening files, reading from files, end of file, writing to files, closing files, reading and writing files, appending a file are given in this lecture to explain the concept behind file handling. This concept exist for all languages like java and c++.

Typology: Slides

2012/2013

Uploaded on 10/24/2013

shreyaa
shreyaa 🇮🇳

4.3

(149)

158 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Files in C (1/2)
In C, each file is simply a sequential stream of bytes. C
imposes no structure on a file.
A file must first be opened properly before it can be
accessed for reading or writing. When a file is opened, a
stream is associated with the file.
Successfully opening a file returns a pointer to (i.e., the
address of) a file structure, which contains a file
descriptor and a file control block.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download File Handling in Programming - Programming - Lecture Slides and more Slides C programming in PDF only on Docsity!

Files in C (1/2)

^ In C, each file is simply a sequential stream of bytes. Cimposes no structure on a file. ^ A file must first be opened properly before it can beaccessed for reading or writing. When a file is opened, astream is associated with the file. ^ Successfully opening a file returns a pointer to (i.e., theaddress of) a file structure, which contains a filedescriptor and a file control block.

Files in C (2/2)

^ The statement:

FILE *fptr1, *fptr2 ;

declares that

fptr

and

fptr

are pointer variables of type

FILE. They will be assigned the address of a filedescriptor, that is, an area of memory that will beassociated with an input or output stream.  Whenever you are to read from or write to the file, youmust first open the file and assign the address of its filedescriptor (or structure) to the file pointer variable.

Testing for Successful Open

^ If the file was not able to be opened, then the valuereturned by the

fopen

routine is NULL.

^ For example, let's assume that the file

mydata

does not

exist. Then:

FILE *fptr1 ;fptr1 = fopen ( "mydata", "r") ;if (fptr1 == NULL){

printf ("File 'mydata' did not open.\n") ; }

Reading From Files

^ In the following segment of C language code:

int a, b ;FILE *fptr1, *fptr2 ;fptr1 = fopen ( "mydata", "r" ) ;fscanf ( fptr1, "%d%d", &a, &b) ;

the^

fscanf

function would read values from the file "pointed" to by

fptr

and assign those values to

a^ and

b.

End of File (2/2)

^ There are a number of ways to test for the end-of-filecondition. Another way is to use the value returned bythe

fscanf

function: int istatus ;istatus = fscanf (fptr1, "%d", &var) ;if ( istatus == EOF ){

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

Writing To Files

^ Likewise in a similar way, in the following segment of Clanguage code:

int a = 5, b = 20 ;FILE *fptr2 ;fptr2 = fopen ( "results", "w" ) ;fprintf ( fptr2, "%d %d\n", a, b ) ;

the fprintf functions would write the values stored in

a^ and

b^ to the file "pointed" to by

fptr

Reading and Writing Files (1/2)#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") ;fprintf (outfile, "%6.2f%2d%5.2f", a, b, c) ;fclose (outfile) ;

read_write.c

Reading and Writing Files (2/2)infile = fopen ("testdata", "r") ;fscanf (infile,"%f %d %f", &e, &f, &g) ;printf ("%6.2f,%2d,%5.2f\n", e, f, g) ;fclose(infile) ;}

Appending Example#include <stdio.h>int main(){ FILE fp;fp=fopen("file.txt","a");fprintf(fp,"%s","This is just an example :)\n"); /append some text*/fclose(fp);return 0;}

append.c