You are on page 1of 13
Files is collection of records (or) it is a place on hard disk, where data is stored permanently. Types of Files There are two types of files in C language which are as follows - = Text file 1 Binary File Text File It contains alphabets and numbers which are easily understood by human beings. An error in a text file can be eliminated when seen. oO fr In text file, the text and characters will store one char per byte. c For example, the integer value 4567 will occupy 2 bytes in memory, but, it will occupy 5 bytes in text file. The data format is usually line- oriented. Here, each line is a separate command. Binary file = It contains 1's and 0's, which are easily understood by computers. Binary file 4 It contains 1's and 0’s, which are easily understood by computers. 2 The error in a binary file corrupts the file and is not easy to detect. 1 In binary file, the integer value 1245 will occupy 2 bytes in memory and in file. 1 A binary file always needs a matching software to read or write it. 1 For example, an MP3 file can be produced by a sound recorder or audio editor, and it can be played in a music player. = MP3 file will not play in an image viewer or a database software. Files are classified into following 2 Sequential files - Here, data is stored and retained in a sequential manner. 1 Random access Files - Here, data is stored and retrieved in a random way. 1. Differences between Text and Binary file = A text file stores data in the form of alphabets, digits and other special symbols by storing their ASCII values and are ina human readable format. For example, any file with a .txt, .c, etc extension. Whereas, a binary file contains a sequence or a collection of bytes which are not ina human readable format. For example, files with .exe, .mp3, etc extension. It represents custom data. Asmall error in a textual file can be recognized and eliminated when seen. Whereas, a small error in a binary file corrupts the file and is not easy to detect. Since the data is not human readable it also adds to the security of the content as one might not be able to get data if the structure is not known. Now, when it comes to programming there are three major differences between the two, i.e., Handling of newlines, storage ot numbers and representation of EOF(End of File). Let's look into these differences in detail: 2. Handling of Newlines Newline is the end of the line or line ending or line break. It is usually a special character which signifies the end of the line. A newline character in a text file is first converted into a carriage return-linefeed combination and then written to the disk. Similarly, when read by a text file the carriage return-linefeed combination is converted into a newline. However, in a binary file, no such conversions take place. 3. Storage of Numbers In the text mode, the function fprintf() is used to store numerical data in the disk. The texts and the characters are stored one character per byte as it should be (char occupies 1 byte in the memory) and as expected the integers should occupy 4 bytes(depends on the compiler) per number. But this is not the case. For example, we have a number 567392. According to integer storage convention, it should occupy 4 bytes in the disk but it does not. It occupies 6 bytes,i.e., 1 byte for every digit in the number. Also, the number 56.9057 will occupy 7 bytes in the disk. Thus, we see that each digit in the file is treated as a character in itself and occupies more space than necessary. So, if we have a lot of numerical data then using a text file will not be very memory efficient( but still the syntax used depends on our usage i.e. if we have uses in which a human has to read the file then we can never choose the binary type). 4. Representation of EOF Another way the text mode and the binary mode can be distinguished is on the basis of the representation of the end-of-file(EOF). In the text mode, a special character with the ASCII code 26 is inserted at the end of the file. This character when encountered returns the EOF signal to the program. This is not the case in binary mode. In the binary mode, we do not have any special character to signify the EOF. It keeps track with the help of the number of characters present in the directory entry of the file. How to create a file in C? C programming provides built-in support to create, read, write and append data to file. To perform any operation on file we use a built-in FILE structure. You need to create pointer to FILE type. The pointer to FILE type will hold a logical reference to our physically existed file on disk (hard disk). In this post | will only explain how to create a file and write data into file. Step by step descriptive logic to create a file and write data into file. 1.Declare a FILE type pointer variable to store reference of file, say FILE * fPtr = NULL;. 2.Create or open file using fopen() function. fopen() function is used to open a file in different mode. You can open a file in basic three different mode _ r (read), w (write) and a (append) mode. We will use w_ file mode to create a file. fopen("file-name", "read-mode"); function accepts two parameter first file name to read/create/write/append data, next is file open mode. On success it return pointer to FILE type otherwise NULL pointer. 3. Input data from user to write into file, store it to some variable say data . 4.C provides several functions to perform 10 operation on file. For this post to make things simple | will use fputs() function to write data to file. fputs("content-to-write", stream) function accepts two parameters. First string data to write into file, next pointer to FILE type that specifies where to write data. “Sp@cCiieéS WNETE TO WITTE dtd. i (iti‘“‘OSOCOCOCCC Use fputs() function to write data to fPtr i.e. perform fputs(data, fPtr);. 5. Finally after completing all operations you must close file, to save data written on file. Use fclose(fPtr) function to close file. | have restricted context of this post to create a file and store data into file. Hence there will be no output on console. Alternatively, you can view file contents by opening the newly created file in your favourite text editor. C programming supports different file open mode to perform different operations on file. To append data into a file youcan use a file open mode. Step by step descriptive logic to append data into a file. Input file path from user to append data, store it insome variable say filePath . Declare a FILE type pointer variable say, fPtr Open file in a (append file) mode and store reference to fPtr using fPtr = fopen(filePath, "a");. Input data to append to file from user, store it to some variable say dataToAppend . Write data to append into file using fputs(dataToAppend, fPtr); . Finally close file to save all changes. Use fclose(fPtr);. Random Access To File There is no need to read each record sequentially, if we want to access a particular record.C supports these functions for random access file processing. 1. fseek() 2. ftell() 3. rewind() fseek(): This function is used for seeking the pointer position in the file at the specified byte. Syntax: fseek( file pointer, displacement, pointer position); Where file pointer ---- It is the pointer which points to the file. displacement ---- It is positive or negative.This is the number of bytes which are skipped backward (if negative) or forward( if positive) from the current position.This is attached with L because this is a long integer. Pointer position: This sets the pointer position in the file. Value pointer position 0 Beginning of file. 1 Current position 2 End of file Ex: 1) fseek( p,10L,0) 0 means pointer position is on beginning of the file,from this statement pointer position is skipped 10 bytes from the beginning of the file. 2)fseek( p,5L,1) 1 means current position of the pointer position.From this statement pointer position is skipped 5 bytes forward from the current position. 3)fseek(p,-5L,1) From this statement pointer position is skipped 5 bytes backward from the current position. ftell() This function returns the value of the current pointer position in the file. The value is count from the beginning of the file. Syntax: ftell(fptr); Where fptr is a file pointer. rewind() This function is used to move the file pointer to the beginning of the given file. Syntax: rewind( fptr); Where fptr is a file pointer. Example program for fseek(): Write a program to read last ‘n’ characters of the file using appropriate file functions(Here we need fseek() and fgetc()). 01 | #include 02 | #include 03 | void main() o4) { 05 FILE *fp; 06 char ch; 07 clrscr(); 038 fp=fopen("filel.c", "r"); 09 if(fp-==NULL) 10 printf("file cannot be opened"); dl: else 12 { 13 printf("Enter value of n to read last ‘n’ characters"); 14 scanf("%d",&n); 15 fseek(fp,-n,2); 16 while((ch=fgetc(fp) )!=EOF) ay { 18 Prantl (UcNcmAchor 19 } 20 } 21 fclose(fp) ; 22 getch(); 234} OUTPUT: It depends on the content in the file.

You might also like