File :
We have been using the function such as scanf and printf read and write data. These are console oriented I/O functions which always use the terminal (keyboard and screen) as the target. This works fine as long as the data is small. However, the many real-life problems involve large volumes of data and in such situation, the console oriented I/O operation pose two major problems.
1. It becomes cumbersome and time consuming to handle large volumes of data through terminals.
2.The entire data is lost when either the program is terminated or the computer is turned off.
C supports a number of functions that have the ability to perform basic file operations, which include :
Naming a file, | |
Opening a file, | |
Reading data from a file, | |
Writing data to a file, and | |
closing a file. |
There are many other functions. Not all of them are supported by all compilers.
1 | fopen() | Creates a new file for use OR Opens an existing file for use. Syntax : FILE *fp; fp=fopen("filename", "mode"); Example : FILE *fp; fp=fopen("data","r"); -The first statement declares the variable fp as a "pointer to the data type FILE". -The second statement also specifies the purpose of opening this file. Themode does this job.
| |||||||||||||||||||||||
2 | fclose() | A file must be closed as soon as all operations on it have been completed. This ensures that all outstanding information associated with file is flushed out from the buffers and all links to the file are broken. It also prevents any accidental misuse of the file. In case , there is a limit to the number of files that can be kept open simultaneously, closing of unwanted files might help open the required files. Another instance where we have to close a file is when we want to reopen the same file in different mode. Syntax : fclose(file_pointer); Example : FILE *p1,*p2; p1=fopen("input","w"); p2=fopen("output","r"); fclose(p1); fclose(p2); | |||||||||||||||||||||||
3 | putc() | Assume that a file is opened with mode w and file pointer fp1. Syntax : putc(char_variable,file_pointer); Example : putc(c,fp1); writes the character contained in the character variable c to the file associated with FILE pointer fp1.The file pointer moves by one character position for every operation of putc. | |||||||||||||||||||||||
4 | getc() | Getc is used to read a character from a file that has been opened in read mode. Syntax : char_variable=getc(file_pointer); Example : c=getc(fp2); The file pointer moves by one character position for every operation of getc. | |||||||||||||||||||||||
5 | putw() | putw is an integer-oriented functions. Assume that a file is opened with mode w and file pointer fp. Syntax : putw(integer_var,file_pointer); Example : putw(a,fp); writes the integer value to the file associated with FILE pointer fp. | |||||||||||||||||||||||
6 | getw(fp) | getw is an integer-oriented functions. Syntax : integer_var=getw(file_pointer); Example : c=getw(fp2); Getw is used to read a integer value from a file that has been opened in read mode. | |||||||||||||||||||||||
7 | fprintf() | Writes a set of data values to a file. Syntax : fprintf(fp,"control string",list); Example : fprintf(fp,"%s%d%f",name,age,7.5); Where fp is a file pointer associated with a file that has been opened for writing.The control string contains output specifications for the items in the list.The list may include variables, constants and strings. | |||||||||||||||||||||||
8 | fscanf() | Reads a set of data values from a file. Syntax : fscanf(fp,"control string",list); Example : fscanf(fp,"%s %d",item,&quantity); This statement would cause the reading of the items in the list from the file specified by fp, according to the specifications contained in the control string. | |||||||||||||||||||||||
9 | fseek() | Sets the position to a desired point into the file. Syntax : fseek(file_ptr,offset,position); file_ptr is a pointer to the file concerned, offset is a number or variable of type long, and position is an integer number. The offset specifies the number of positions bytes to be moved from the location specified by position. The position can take one of the following three values.
Example :
| |||||||||||||||||||||||
10 | ftell() | Gives the current position in the file.(in terms of bytes from the start). Syntax : long_var=ftell(file_ptr); Example : n=ftell(fp); ftell takes a file pointer and returns a number of type long, that corresponds to the current position. This function is useful in saving the current position of file, Which can be used later in the program. | |||||||||||||||||||||||
11 | rewind() | Sets the position to the beginning of the file. Syntax : rewind(fp); Example : rewind(fp); n=ftell(fp); would assign 0 to n because the file position has been set to the start of the file by rewind. This function helps us in reading a file more than once, without having to close and open the file. |
Command line arguments :
It is a parameter supplied to a program when the program is invoked. This parameter may represent a filename the program should process. For example if we want to execute a program to copy the contents of file named abc to another one named xyz, then we may use a command like
main can take two arguments called argc and argv and the information contained in the command line is passed on to the program through these arguments, when main is called up by the system.
The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument vector and represents an array of character pointers that point to the command line arguments. The size of this array will be equal to the value of argc. For instance, for the command line given above, argc is three and argv is an array of three pointers to strings.