Sunday 28 January 2018

File I/O


File is a collection of bytes that are stored on secondary storage devices like a disk.

 

Why files are needed?

  • 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 have 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 few commands in C.
  • You can easily move your data from one computer to another without any changes.

There are two kinds of files in a system. They are,

 

1.Text files (ASCII)--> Text files contain ASCII codes of digits, alphabetic and symbols.

Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.

When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete the contents.

They take minimum effort to maintain, are easily readable, and provide least security and takes bigger storage space.


2.Binary files-->Binary file contains collection of bytes (0’s and 1’s). Binary files are compiled version of text files.


Binary files are mostly the .bin files in your computer.

Instead of storing data in plain text, they store it in the binary form (0's and 1's).

They can hold higher amount of data, are not readable easily and provides a better security than text files.


So,In this tutorial, you'll learn how to do file IO, text and binary in C, using fopen, fwrite, and fread, fprintf, fscanf, fgetc and fputc.


For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed.


For example :FILE *fp;




File Operation in C Procedure:

There are 4 basic operations that can be performed on any files in any programming language. They are,


  • Opening/Creating a file
  • Closing a file
  • Reading a file
  • Writing in a file


Steps for Processing a File

  • Declare a file pointer variable.
  • Open a file using fopen() function.
  • Process the file using the suitable function.
  • Close the file using fclose() function.




Functions with their uses:

Function
Uses/Purpose
Opens a file.
Closes a file.
Reads a character from a file
Writes a character to a file
Read integer
Write an integer
Prints formatted output to a file
Reads formatted input from a file
Read string of characters from a file
Write string of characters to file
Detects end-of-file marker in a file





Opening a file:


You can use the fopen( ) function to create a new file or to open an existing file.
Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.


FILE *fopen(const char *filename, const char *mode);


Here, filename is a string literal, which you will use to name your file, and access mode can have one of the following values −



r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)


Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will return 0, the NULL pointer.






Here's a simple example of using fopen:
FILE *fp;
fp=fopen("c:\\suraj.txt", "r");


This code will open test.txt for reading in text mode.


If you are going to handle binary files, then you will use following access modes instead of the above mentioned ones −


"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"


Closing a File:


The file (both text and binary) should be closed after reading/writing.


Closing a file is performed using library function fclose().


fclose(fptr); //fptr is the file pointer associated with file to be closed.


The fclose(-) function returns zero on success, or EOF if there is an error in closing the file. This function actually flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h.



Reading and writing to a text file


It is also possible to read (or write) a single character at a time--this can be useful if you wish to perform character-by-character input (for instance, if you need to keep track of every piece of punctuation in a file it would make more sense to read in a single character than to read in a string at a time.) The fgetc function, which takes a file pointer, and returns an int, will let you read a single character from a file:

int fgetc (FILE *fp);

Notice that fgetc returns an int. What this actually means is that when it reads a normal character in the file, it will return a value suitable for storing in an unsigned char (basically, a number in the range 0 to 255). On the other hand, when you're at the very end of the file, you can't get a character value--in this case, fgetc will return "EOF", which is a constant that indicates that you've reached the end of the file. 


The fputc function allows you to write a character at a time--you might find this useful if you wanted to copy a file character by character. It looks like this:

int fputc( int c, FILE *fp );

Note that the first argument should be in the range of an unsigned char so that it is a valid character. The second argument is the file to write to. On success, fputc will return the value c, and on failure, it will return EOF.



Binary file I/O - fread and fwrite


For binary File I/O you use fread and fwrite.

The declarations for each are similar:

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
              
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);


Both of these functions deal with blocks of memories - usually arrays. Because they accept pointers, you can also use these functions with other data structures; you can even write structs to a file or a read struct into memory.

Let's look at one function to see how the notation works.

fread takes four arguments. Don't be confused by the declaration of a void *ptr; void means that it is a pointer that can be used for any type variable. The first argument is the name of the array or the address of the structure you want to write to the file. The second argument is the size of each element of the array; it is in bytes. For example, if you have an array of characters, you would want to read it in one byte chunks, so size_of_elements is one. You can use the sizeof operator to get the size of the various datatypes; for example, if you have a variable int x; you can get the size of x with sizeof(x);. This usage works even for structs or arrays. E.g., if you have a variable of a struct type with the name a_struct, you can use sizeof(a_struct) to find out how much memory it is taking up.

e.g., 
sizeof(int);

The third argument is simply how many elements you want to read or write; for example, if you pass a 100 element array, you want to read no more than 100 elements, so you pass in 100.

The final argument is simply the file pointer we've been using. When fread is used, after being passed an array, fread will read from the file until it has filled the array, and it will return the number of elements actually read. If the file, for example, is only 30 bytes, but you try to read 100 bytes, it will return that it read 30 bytes. To check to ensure the end of file was reached, use the feof function, which accepts a FILE pointer and returns true if the end of the file has been reached.

fwrite is similar in usage, except instead of reading into the memory you write from memory into a file.

For example,
FILE *fp;
fp=fopen("c:\\test.bin", "wb");
char x[10]="ABCDEFGHIJ";
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);





C Programms:


 FILE OPEN, FILE WRITE AND FILE CLOSE IN C LANGUAGE:




 Program to merges two files and stores their contents in another file.







Programm to find  number of lines in a file 





2 comments:

  1. So, Where we use tellg,tellp,seekg,seekp in above file programs??

    ReplyDelete
    Replies
    1. Dear Vivek,

      We use tellg,tellp,seekg,seekp in C++.

      In C++ we have a get pointer and a put pointer for getting (i.e. reading) data from a file and putting(i.e. writing) data on the file respectively.

      seekg() is used to move the get pointer to a desired location with respect to a reference point.

      tellg() is used to know where the get pointer is in a file.

      seekp() is used to move the put pointer to a desired location with respect to a reference point

      tellp() is used to know where the put pointer is in a file.

      Delete