File in c

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

BY ANKAN BANDYOPADHYAY:

BY ANKAN BANDYOPADHYAY FILE MANAGEMENT IN C

What is File ??:

What is File ?? In C, a file may be anything from a disk file to a terminal or printer In C, a file is a sequential stream of characters In C all I/O devices are treated as files. the standard input device and the standard output device are treated as files

Basic File Operations :

Basic File Operations Creation of a new file Opening an existing file Reading from a file Writing to a file Moving to a specific location in a file (seeking) Closing a file

Example: To read a File:

Example: To read a File # include <stdio.h> main( ) { FILE *fp ; char ch ; fp = fopen ( “abc.txt", "r" ) ; if ( fp == NULL ) { puts ( "cannot open file" ) ; exit( ) ; } while ( 1 ) { ch = fgetc ( fp ) ; if ( ch == EOF ) break ; printf ( "%c", ch ) ; } fclose ( fp ) ; } fp: file pointer File name Opening mode Checking for End of the file or not

Slide 7:

Opening Mode "r" Function Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen( ) returns NULL. Operations possible – reading from the file.

Slide 8:

Opening Mode “w" Function Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. Operations possible – writing to the file.

Slide 9:

Opening Mode “a" Function Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. Operations possible - adding new contents at the end of file.

Slide 10:

Opening Mode "r+" Function Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. Operations possible - reading existing contents, writing new contents, modifying existing contents of the file.

Slide 11:

Opening Mode "w+" Function Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open file. Operations possible - writing new contents, reading them back and modifying existing contents of the file.

Slide 12:

Opening Mode "a+" Function Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file. Operations possible - reading existing contents, appending new contents to end of file. Cannot modify existing contents.

Demonstration of fputc():

Demonstration of fputc() Write into a file character by character Signature: fs = fopen ( "pr1.txt", "r" ) ; ft = fopen ( "pr2.txt", "w" ) ; while ( 1 ) { ch = fgetc ( fs ) ; if ( ch == EOF ) break ; else fputc ( ch, ft ) ; }

Demonstration of fgets() & fputs():

Demonstration of fgets() & fputs() fgets() : read string from file fgets (address where the string is stored , maximum length of the string , file pointer ); fputs() : write string into a file fputs ( string, file pointer ) ;

Example: For fputs():

Example: For fputs() #include <stdio.h> main( ) { FILE *fp ; char s[80] ; fp = fopen ( "POEM.TXT", "w" ) ; if ( fp == NULL ) { puts ( "Cannot open file" ) ; exit( ) ; } printf ( "\nEnter a few lines of text:\n" ) ; While ( strlen ( gets ( s ) ) > 0 ) { fputs ( s, fp ) ; fputs ( "\n", fp ) ; } fclose ( fp ) ; }

Example: For fgets():

Example: For fgets() #include <stdio.h> main( ) { FILE *fp ; char s[80] ; fp = fopen ( "POEM.TXT", "r" ) ; if ( fp == NULL ) { puts ( "Cannot open file" ) ; exit( ) ; } while ( fgets ( s, 79, fp ) != NULL ) printf ( "%s" , s ) ; fclose ( fp ) ; }

Example: For fprintf():

Example: For fprintf() #include <stdio.h> main( ) { FILE *fp ; char another = 'Y' ; struct emp { char name[40] ; int age ; float bs ; } e; fp = fopen ( "EMPLOYEE.DAT", "w" ) ; if ( fp == NULL ) { puts ( "Cannot open file" ) ; exit( ) ; } while ( another == 'Y' ) { printf ( "\nEnter name, age and basic salary: " ) ; scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ; fprintf ( fp, "%s %d %f\n", e.name, e.age, e.bs ) ; printf ( "Add another record (Y/N) " ) ; fflush ( stdin ) ; another = getche( ) ; } fclose ( fp ) ; }

Example: For fscanf():

Example: For fscanf() #include <stdio.h> main( ) { FILE *fp ; struct emp { char name[40] ; int age ; float bs ; } e; fp = fopen ( "EMPLOYEE.DAT", "r" ) ; if ( fp == NULL ) { puts ( "Cannot open file" ) ; exit( ) ; } while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs ) != EOF ) printf ( "\n%s %d %f", e.name, e.age, e.bs ) ; fclose ( fp ) ; }

Demonstration of fseek():

Demonstration of fseek() Shifts the file pointer from the current position (forward or backward) Syntax: fseek(fp, ± moving bytes, position) - shifts the file pointer by the number of bytes in the file from the position specified

Slide 20:

Thank u…

authorStream Live Help