Unit 8

468 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Unit 8

  1. 1. UNIT 8 FILESFile: A file is a place on the disk where a group of related data is stored. It is a permanent named unit that holdslarge volume of related data. It stores the data in secondary or auxiliary memory such as CDs, tapes and Hard disk.Hence, it is permanently stored memory. It is used to support the volatile nature of main memory. That is, when thesystem is shutdown, main memory looses the data and relies on secondary storage. When the system is switched on,main memory gets loaded with the data from the files. In addition to reading data from files, main memory will alsowrite data into the files if it has made any changes to the data. In addition to reading data from files, main memory will also write data into the files if it has made anychanges to the data. A buffer is such a temporary storage used for data transfers between main memory andsecondary memory.File name: The file name is typically a string of characters. The file names can be “Data.txt”, ”mul.txt”, ”prg.c” or“Div.n” etc. A file name may contain two parts, a primary name and optional period with the extension.Ex: input.data, student.c etcFile Information Table: In order to read or write a file in a program, certain information is required like file name,the location of the current character of the file etc. such information is stored in a predefined structure called fileinformation table. This table is defined by “stdio.h” header file with “FILE” as its identifier. Hence, whenever a fileis to be read or written, it is declared using the „FILE‟ type.Stream: “A stream is a flow of data bytes between a program and files.” A stream can be either a source of data (incase of a write operation). The write-end of a stream is called an input stream. The read-end of a stream is called an output stream. „C‟ language provides stream as the basic for all types of operations on files. The files generally are stored onsome secondary storage. Such as disks, tapes etc. When a „C‟ program has to operate on files it uses standard libraryfunctions declared in header file stdio.h. All those functions either read or write data from or to the file throughstreams i.e., All input output related functions use streams as the communication.Redirecting I/O to a File : Both read and write file operations can be easily performed under many operating systems. If we want to writeprogram results into a file, then command. prog > data, will execute the program with the name „prog‟ and writes the output to the file called data. We can redirect input from file also Ex : reverse <file1 This command takes the value from file „file1‟ and redirects to program called „reverse‟. We can redirect input and output to the program at the same time. Ex : reverse < number > data This command reads all the input for the program „reverse‟ from file „number‟ and write all the output to the file„data‟.End of file : And end-file condition exists when the final piece of data has been read from a file. Attempting to read after endof file might cause the program to terminate with an error (or) it might cause the program to go into an infinite loop.EOF() function is defined in the standard I/O include file <stdio.h> Special functions for working with FilesFollowing are the main steps involved file processing 1. Opening File 2. Processing a File 3. Closing a File
  2. 2. Defining file Pointer : A file is opened, accessed, closed through a file pointer. A file pointer is declared using „FILE‟, which is definedin <stdio.h>. FILE *name of the file pointer; Ex : FILE * fp;The fopen () function : Before doing any I/O operation on a file, the file must be opened. To open a file, we must specify the name ofthe file. The function fopen() is used to open (or) create a file. The general format of the fopen() function is fopen (“filename”, “mode); The function fopen () takes two arguments i) filename ii) system type operation i.e. modeModes : A file may be opened in one of the following modes„w‟ mode This mode opens the file in write mode. Only file write is possible„r‟ mode This mode opens the file in read mode. Only file read is possible„a‟ mode This mode opens the file in append mode. In this mode data is added from the end of the existing data.„w+‟ mode Write mode with read option„r+‟ mode read mode with write option„a+‟ mode append mode with write option If we add „b‟ i.e „wb‟, „rb‟ etc., the file will be opened in binary mode for corresponding system operation. Ex : fp =fopen(“student.dat”, “w”); This statement opens a file “student.dat” in write mode and return the file address into file pointer „fp‟. The fopen() function opens the file, but if file is not existing the function creates a file.The getc() and putc() Functions :getc() : The getc() function inputs one character from a specified file stream. The syntax of getc() is : char getc (FILE * stream); Ex : char flag ; flag = get c (fp); This statement inputs a character from the file defined by fp.putc() : The putc() function outputs one character to the file stream represented by the specified file pointer. Thesyntax of putc() is : char putc (char ch, FILE * stream); Ex : putc (flag, fp); This statement writes the character „flag‟ to file pointed by „fp‟The fclose () Function : When operations are completed on a file, it is good habit to close the file. When a program terminates normally,the system automatically closes any open files. It is generally better programming practice to close a file, as soon as weare done with it. fclose() function is used to close an opened file. The general format of fclose() is :
  3. 3. fclose (filepointer); The argument to the fclose () is filepointer. Ex : fclose (fp); Closes the file defined by the file pointer „fp‟.The feof () Function : The feof() function checks the file position indicator to determine if the end of the file associated with stream hasbeen reached. A nonzero value is returned for the end of the file, otherwise zero is returned. Syntax : feof (filepointer); Ex : while (! feof(fp)) { ..... ..... } The loop continues till end of the file.Ex : Following program explains copying contents of one file to another file : #include <stdio.h> main () { FILE *fp1, *fp2; char flag; fp1 = fopen (“Charadata1.data”, “r”); fp2 = fopen (“Charadata2.data”, “w”); while (! feof(fp)) { flag = getc (fp1); putc (flag, fp2); } fclose (fp1); fclose (fp2); getch();} In order to run above program the file chardata1.data should exists i.e. it should be created. The while loopcopies the contents of file chardata1.data to chardata2.data.The fprintf () Function : The fprintf () is like printf ( ) except that this function writes the data to a specified file. This function takes anadditional parameter, which is the FILE pointer that identifies the file to which data is to be written. Ex : fprintf (fp, “programming in c is fun n”); This statement writes the specified string into a file identified by the file pointer fp.The fscanf () Function : This function is used to read data from a file. The general format is fscanf (File pointer, “control characters”, & variablenames); Ex : fscanf (fp, “%d %f”, &a, &b); This statement reads an integer and float value assigns them to variables „a‟ and „b‟ respectively from a fileidentified by „fp‟.The fgets () Function :
  4. 4. fgets() function is used to read a string from a file. The syntax of fgets() is as shown below: char *fgets(char *str, int num, FILE * stream); The fgets() function reads upto „num‟ characters from a file pointed by „stream‟ and places them into thecharacter array pointed to by „str‟. Characters are read until a new line or until the specified limit is reached.Ex: fgets(str, 20, fp); This statement reads a string of length 20 pointed by filepointer „fp‟ to the character array „str‟.The fputs () Function : fputs() function is used to write a string on to the file. The syntax of the fputs() frunction is : fputs(char *str, FILE *stream);fputs() function writes the contents of the string pointed to by „str‟ to the specified stream. The null terminator is notwritten.Ex: fputs(str, fp); This statement writes the string „str‟ in to the file pointed by the file pointer fp.Ex : Programs to perform file operations using fgets() and fputs :# include “stdio.h”main (){ FILE *fp; int n; char str[5]; printf(“n Enter No.of Strings :”); scanf(“%d”, &n); fp =fopen(“Names.dat”, “w”); for (i=1; i<=n; i++) { scanf (“%s”, str); fputs (str, fp); } fclose (fp);}The above program creates a file “names.dat” and writes „n‟ strings using fputs.main(){ FILE *fp; char str[5]; fp = fopen (“names.dat”, “r”); while (!feof(fp)) { fgets(str, 5, fp); prints(“n%s”, str); }fclose (fp);} This program opens the file names.dat in read mode. The while loop reads string by string and display them onthe screen, until end of the file.stdin, stdout, stderr : In „C‟ when the program begins execution, the following three files will be automatically opened.
  5. 5. 1. Standard input, stdin 2. Standard output, stdout 3. Standard error, stderr By default, the stdin, stdout, stderr refer to user‟s console. This means that whenever a program expects inputfrom the standard input, it receives that input from stdin. A program that writes to the standard output prints it data tostdout. Any error messages that are generated by the library routines are sent to stderr.fwrite() : This function is used to write a record (or) structure to the file. fwrite(&s, sizeof(structure),1,fp); Structure „s‟ will be stored into the file pointed by fp.fread() : This function is used to read a record / structure from the file. fread(&s, sizeof(structure),1,fp); Structure „s‟ will be read from the file pointed by fp.ftell () : This function is used to locate the position of the file pointer. It returns a long integer. Ex : Long int L; L=ftell (fp);fseek() : This function is used to set the file pointer to the desired position. fseek( fp, ibytes, pos);rewind() : This function sets the file pointer to the initial position of the file. Ex : rewind(fp).The exit() Function : To explicitly terminate a program, the function exit () will be called. exit (n) ; has the effect of terminating the current program. Any open files are automatically closed by thesystem. The integer „n „ is called exit status.Renaming and Removing Files : rename () function is used to rename the existing file. Ex : rename (“tempfile”, “database”); This statement renames the file “tempfile” to “database”. remove () function deletes the file specified by its argument. Ex : remove (“tempfile”);This statement deletes the file “tempfileRandom Accessing Files : Generally a file is accessed sequentially. For random access data „C‟ supports different functions.ftell () : This function is used to locate the position of the file pointer. It returns a long integer. Ex : Long int L; L=ftell (fp);fseek() : This function is used to set the file pointer to the desired position. fseek( fp, ibytes, pos);rewind() : This function sets the file pointer to the initial position of the file. Ex : rewind(fp).To access the record randomly : i. Read the present file pointer position using ftell().
  6. 6. ii. Set the file pointer to initial position using rewind(). iii. Set the file pointer to required position by using fseek().Text files and Binary files: The text files are those files that contain letters, digits and symbols. A text file mainly contains characterscoded from the ASCII character set. On the other hand, a binary file is a file that uses all eight bits of bytes forstoring information. The text file is mainly in a form, which can read and understood by human beings. The binaryfile, on the other hand, is generally in a form, which can be interpreted and understood by a computer system. In contract to a binary file, the text file uses only seven bits allowing the eighth bit to be zero. In acomputer system, all executable or .exe files. Dynamic Link Library(DLL) and many database files are stored asbinary files. One of the differences between text and binary file is that the data contained in the text file can be readby a word processor while the binary file data cannot be read by a word processor. In „C‟ when you access a file forreading and writing then you can choose to open the file as binary or text file. „C‟ programming language providesthe fopen() function to open a file in a specific mode. When we specify the mode with the fopen() function, you can choose to open the file in a read mode forreading, write mode for appending data to the end of a file. It is also determines at the time of opening a file usingfopen() function that whether the file should be opened as a binary or text file. The „C‟ programming languageprovides different read, write and append modes for reading and writing to text and binary files. If you use thefollowing modes with fopen() function then the file is opened as a text file.  r: this mode allows you to open a file as a text file for reading data from it.  w: this mode allows you to open a file as a text file for writing data to it.  a: this mode allows you to open a file as a text file for appending data at the end of the file.  r+: this mode allows you to open a file as a text file for reading data as well as writing data to a file.  w+: this mode allows you to open a file as a text file for writing data as well as reading data from a file.  a+: this mode allows you to open a file as text file for both reading and writing data to a file. We should add „b‟ for binary file with the r,w and a modes to a file as a binary file, you can use the following modes:  rb: this mode allows you to open a file as a binary file for reading data from it.  wb: this mode allows you to open a file as a binary file for writing data to it.  ab:this mode allows you to open a file as a binary file for appending data at the end of the file.  rb+: this mode allows you to open a file as a binary file for reading as well as writing data to a file.  wb+: this mode allows you to open a file as a binary file for writing data as well as reading data from a file.  ab+: this mode allows you to open a file as binary file for both reading and writing data to a file. In „C‟, the process for writing of text and binary files is also different. In case of a binary file, data is containedas lines of text. An end of line marker is automatically added we indicate that the end of the line is reaches. Thefwrite() function of „C‟ allows you to write an end of line marker at the end of text in a text file. However in case ofa binary file, the data is not separated and hence no end of line marker is written at the end of the text when andwriting data to a binary file using fwirte() function. The reading process for text and binary files is also is alsodifferent. In case of text file, the data is separated into lines of text as it is read according to the position of the end ofthe line marker. On the other hand, the data in a binary file is not separated into lines of text.

×