File Handling in C Programming CS1001 Structured Programming

  • Slides: 26
Download presentation
File Handling in C Programming CS-1001 Structured Programming Approach

File Handling in C Programming CS-1001 Structured Programming Approach

Topic to be cover • Introduction To File And C File Handling. • Need

Topic to be cover • Introduction To File And C File Handling. • Need Of File Handling • Types Of Files • File Operations • File Pointer • Opening And Creating File Using fopen() Function. • Closing A File Using fclose( ) Function. • File Input /Output Library Functions. • Reading From Files • Writing Into File

1. Introduction to File and File Handling Introduction to file ü A file contains

1. Introduction to File and File Handling Introduction to file ü A file contains collection of related data/information or of bytes, that are stored permanently in a storage device(Disks). ü The collection of bytes could form a text document with characters, words, lines, paragraphs and pages, or a database with its fields and records, or a graphical image made of pixels, or some other data. File Handling in C Programming ü File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. ü File handling in C refers to the task of storing data in the form of input or output produced by running C programs in data files, namely, a text file or a binary file for future reference and analysis.

2. Why we need file We need file handing in programming due to following

2. Why we need file We need file handing in programming due to following reasons: ü When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates. ü If you need to enter a large number of data, it will take a lot of time to enter them all. However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C. ü You can easily move your data from one computer to another without any changes.

Irrespective of the data they contain, or the methods used to process them, all

Irrespective of the data they contain, or the methods used to process them, all files have certain common properties. ü Every file has a name. ü The file must be opened before we start processing it. Conceptually, until a file is opened no operation can be performed on the file. (When it is opened, we may access it at its beginning or end. To prevent accidental misuse, at the time of opening the file we must tell the system what we intend to do with the file – read, write, or append. ü It must be closed when it is done with. (When we are finished using the file, we must close it. If the file is not closed, the operating system cannot finish updating its own housekeeping records used for file management, and data in the file may be lost. ) ü The third one is about the actual operations on the file. In between the opening and closing of a file, we can write into, or read from, or append to it.

3. Types of files Essentially there are two kinds of files that programmers deal

3. Types of files Essentially there are two kinds of files that programmers deal with v Text files v Binary files. Text File ü Text files are used to store character data. ü A text file consists of a stream of characters that can be processed sequentially. ü A text file is usually opened for only one kind of operation (read, write or append) at any given time. ü Text files only process characters, they can only read or write data one character at a time. ü There are functions that read or write a string or a line of text, but these functions also essentially process the data one character at a time.

Types of files (Conti…. ) Binary Files ü Instead of storing data in plain

Types of files (Conti…. ) Binary Files ü Instead of storing data in plain text, they store it in the binary form (0's and 1’s). ü They can hold a higher amount of data, are not readable easily, and provides better security than text files. ü The major difference is in the way the file is processed. v Binary files can be processed sequentially or using direct access, depending on the needs of the application. v In C, processing a file with a direct access involves moving the current file position to an appropriate place in the file before reading or writing data. v Since it is possible to have access that is not sequential, binary files are generally opened for read and write operations simultaneously.

4. File Operations In C, you can perform four major operations on files, either

4. File Operations In C, you can perform four major operations on files, either text or binary: üCreating a new file or Opening an existing file üClosing a file üReading from (Input ) the file üWriting (output) information to a file

5. File Pointer üWhen working with files, you need to declare a pointer to

5. File Pointer üWhen working with files, you need to declare a pointer to structure of type FILE. üThis declaration is needed for communication between the file and the program. FILE *fp; üHere, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened (or created) file.

6. Opening in file or creating a file ü Before performing any operation with

6. Opening in file or creating a file ü Before performing any operation with file, we must open it. ü For opening a file we must tell to the operating system that we want to open a specific file to perform a particular operation with file contents. ü The fopen() function is used to create a new file or to open an existing file. ü The syntax for opening a file in standard I/O FILE *fopen (const char *filename, const char *mode) or FILE *ptr = fopen("fileopen", "mode");

Opening in file or creating a file (Conti…. ) ü Description üfp – file

Opening in file or creating a file (Conti…. ) ü Description üfp – file pointer to the structure data type “FILE”. üfilename – the actual file name with full path of the file. The filename is any valid filename that the operating system can understand. It is enclosed in double quotes. ümode on the file. Example: r, w, a, r+, w+ and a+.

Opening in file or creating a file (Conti…. ) ü Mode can be of

Opening in file or creating a file (Conti…. ) ü Mode can be of following types, Mode r w a r+ w+ a+ rb wb ab rb+ wb+ Description opens a text file in reading mode opens or create a text file in writing mode. opens a text file in append mode opens a text file in both reading and writing mode opens a binary file in reading mode opens or create a binary file in writing mode opens a binary file in append mode opens a binary file in both reading and writing mode

Opening in file or creating a file (Conti…. ) ü Read Mode v Using

Opening in file or creating a file (Conti…. ) ü Read Mode v Using the r indicates that the file is a text file and rb indicates that the file is a binary file. v Opening a file for reading requires that the file already exists. v If it does not exist, the file pointer variable will be set to NULL by the fopen( ) function. ü Write mode v Using the w indicates that the file is a text file and wb indicates that the file is a binary file. v When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there. ü Append Mode v Using the a indicates that the file is a text file and ab indicates that the file is a binary file. v When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. v If a file exists with that name, the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file.

7. Closing the file ü The fclose() function is used to close an already

7. Closing the file ü The fclose() function is used to close an already opened file. ü General Syntax : int fclose( FILE *fp); ü Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. ü This EOF is a constant defined in the header file stdio. h.

8. File input /output Function Library Function Operation Syntax Example fopen() Create /Open a

8. File input /output Function Library Function Operation Syntax Example fopen() Create /Open a file fptr=fopen(“filename”, ”mode”); FILE *fptr; fptr=fopen(“employee’, ”r” ); fclose() Close an active file fclose(fptr); getc() Read a character from a file che=getc(fptr); FILE *fptr; char che; che=getc(fptr); putc() Write a character to a file putc(che, fptr); FILE *fptr 2; char che; putc(che, fptr 2); getw() Read an integer data from a file che=getw(fptr); FILE *fptr; int n; n=getc(fptr); putw() Write an integer data to a file putw(integer, fptr); FILE *fptr 2; int n; putc(n, fptr 2); feof() When reading from a file it detect the end of file. feof(fptr); fscanf() Read a set of data value from a file. fscanf(fptr, ”control string” , &v 1, & v 2, . . &vn); fscanf(fptr, “%d”, &emp_id); fprintf() Write a set of data value to a file. fprintf(fptr, ”control string” , v 1, v 2, . . vn); fscanf(fptr, “%d”, emp_id);

9. Writing to a File • In C, when you write to a file,

9. Writing to a File • In C, when you write to a file, newline characters 'n' must be explicitly added. • The stdio. h library offers the necessary functions to write to a file: ü fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer. ü fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer. ü fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.

9. 1 fputc ( ) function ü It writes a character to the file

9. 1 fputc ( ) function ü It writes a character to the file pointed to by file_pointer. ü Syntax fputc(char, file_pointer) ü Example #include <stdio. h> int main() { int i; FILE * fptr; char fn[50]; char str[] = "Sir Padampat Singhania Universityn"; fptr = fopen("test. txt", "w"); for (i = 0; str[i] != 'n'; i++) { /* write to file using fputc() function */ fputc(str[i], fptr); } fclose(fptr); return 0; } // "w" defines "writing mode"

9. 2 fputs ( ) function ü It writes a string to the file

9. 2 fputs ( ) function ü It writes a string to the file pointed to by file_pointer. ü Syntax fputs(str, file_pointer) ü Example #include <stdio. h> int main() { FILE * fp; fp = fopen("fputs_test. txt", "w+"); fputs("Sir Padampat Singhania Universityn", fp); fputs("Department of Computer Science and Engineeringn", fp); fputs("CS-1001 -Strucutred Programming Approachn", fp); fclose(fp); return (0); }

9. 3 fprintf ( ) function ü It prints a string to the file

9. 3 fprintf ( ) function ü It prints a string to the file pointed to by file_pointer. ü The string can optionally include format specifiers and a list of variables variable_lists. ü Syntax fprintf(file_pointer, str, variable_lists); ü Example void main(){ char empname[20]; int empno, salary, n=3, i; FILE *sfptr; ptr=fopen("file 3", "w"); printf("n Employee details n"); for(i=0; i<3; i++) { scanf("%d %s %d", &empno, empname, &salary); fprintf(ptr, "%d %s %dn", empno, empname, salary); } fclose(ptr); }

10. Reading from the file There are three different functions dedicated to reading data

10. Reading from the file There are three different functions dedicated to reading data from a text file ü fgetc(file_pointer): ü It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back. ü fgets(buffer, n, file_pointer): ü It reads n-1 characters from the file and stores the string in a buffer in which the NULL character '' is appended as the last character. ü fscanf(file_pointer, conversion_specifiers, variable_adresses): ü It is used to parse and analyze data. ü It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. ü Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.

10. 1 Reading using fgetc() fucntion ü The fgetc() function reads a character from

10. 1 Reading using fgetc() fucntion ü The fgetc() function reads a character from the input file referenced by file pointer. ü The return value is the character read, or in case of any error, it returns EOF. ü Syntax int fgetc( FILE * fp ); ü Example #include <stdio. h> int main(){ FILE *fp 1; char c; // Pointer to the file //Character variable to read the content of file */ fp 1= fopen ("C: \myfiles\newfile. txt", "r"); // /* Opening a file in r mode*/ while(1){ c = fgetc(fp 1); if(c==EOF) break else printf("%c", c); } fclose(fp 1); return 0; }

10. 2 Reading using fgets() fucntion ü The functions fgets() reads up to n-1

10. 2 Reading using fgets() fucntion ü The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. ü Syntax char *fgets(char *buf, int rec_len, FILE *fpr) here ü Buf : Array of characters to store strings. ü rec_len : Length of the input record. ü Fpr : Pointer to the input file.

#include <stdio. h> int main() { FILE *fpr; char str[100]; /*Char array to store

#include <stdio. h> int main() { FILE *fpr; char str[100]; /*Char array to store string */ fpr = fopen("C: \mynewtextfile. txt", "r"); /*Opening the file in "r" mode*/ if (fpr == NULL) /*Error handling for file open*/ { puts("Issue in opening the input file"); } while(1) /*Loop for reading the file till end*/ { if(fgets(str, 10, fpr) ==NULL) break; else printf("%s", str); } fclose(fpr); /*Closing the input file after reading*/ return 0; }

10. 3 Reading using fscanf() fucntion ü It is used to parse and analyze

10. 3 Reading using fscanf() fucntion ü It is used to parse and analyze data. ü It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. ü Keep in mind that as with scanf(), fscanf() stops reading a string when space or newline is encountered. ü Syntax : int fscanf(FILE *fp, const char *format, list of variables);

#include<stdio. h> #include<conio. h> struct emp{ char name[10]; int age; }; main() { struct

#include<stdio. h> #include<conio. h> struct emp{ char name[10]; int age; }; main() { struct emp e; FILE *p, *q; p = fopen(“one. txt”, “a”); q = fopen(“one. txt”, “r”); printf(“Enter Name and Age”); scanf(“%s %d”, e. name, &e. age); fprintf(p, “%s %d”, e. name, e. age); fclose(p); do { fscanf(q, ”%s %d”, e. name, e. age); printf(“%s %d”, e. name, e. age); } getch(); } while(!eof(q));

C program to copy one file into another file #include<stdio. h> void main() {

C program to copy one file into another file #include<stdio. h> void main() { char che, ch; int n; FILE *sfptr, *tfptr; sfptr=fopen("file 1", "r"); tfptr=fopen("file 2", "w"); while(!feof(sfptr)) { che=getc(sfptr); putc(che, tfptr); } printf("n The file copied successfully n"); close(sfptr); close(tfptr); }