File Management
File Management
Management
File management is what you
have, and how you want to
manipulate it. - Anonymous
Why File Management?
In real life, we want to store data permanently so that later we can retrieve it and
reuse it.
A file is a collection of characters stored on a secondary storage device like hard disk,
or pen drive.
There are two kinds of files that programmer deals with:
Text Files are human readable and it is a stream of plain English characters
Binary Files are computer readable, and it is a stream of processed characters and ASCII symbols
Open the file for reading only. If it exists, then the file is opened with the current contents; otherwise
r
an error occurs.
Open the file for writing only. A file with specified name is created if the file does not exists. The
w
contents are deleted, if the file already exists.
Open the file for appending (or adding data at the end of file) data to it. The file is opened with the
a
current contents safe. A file with the specified name is created if the file does not exists.
r+ The existing file is opened to the beginning for both reading and writing.
Note: The main difference is w+ truncate the file to zero length if it exists or create a new file if it doesn't.
While r+ neither deletes the content nor create a new file if it doesn't exist.
File Handling Functions
Basic file operation performed on a file are opening, reading, writing, and closing a
file.
Syntax Description
fp=fopen(file_name, This statement opens the file and assigns an identifier to the FILE type pointer fp.
mode);
Example: fp = fopen("printfile.c","r");
fclose(filepointer); Closes a file and release the pointer.
Example: fclose(fp);
fprintf(fp, Here fp is a file pointer associated with a file. The control string contains items to be
“control string”, printed. The list may includes variables, constants and strings.
list);
Example: fprintf(fp, "%s %d %c", name, age, gender);
File Handling Functions
Syntax Description
fscanf(fp, Here fp is a file pointer associated with a file. The control string contains items to be
“control string”, printed. The list may includes variables, constants and strings.
list);
Example: fscanf(fp, "%s %d", &item, &qty);
int getc( getc() returns the next character from a file referred by fp; it require the FILE pointer
FILE *fp); to tell from which file. It returns EOF for end of file or error.
Example: c = getc(fp);
int putc(int c, putc() writes or appends the character c to the FILE fp. If a putc function is
FILE *fp); successful, it returns the character written, EOF if an error occurs.
EOF EOF stands for “End of File”. EOF is an integer defined in <stdio.h>
Program
1 #include <stdio.h>
2 void main()
3 {
4 FILE *fp; //p is a FILE type pointer
5 char ch; //ch is used to store single character
6 fp = fopen("file1.c","r"); //open file in read mode and store file pointer in p
7 do { //repeat step 9 and 10 until EOF is reached
8 ch = getc(fp); //get character pointed by p into ch
9 putchar(ch); //print ch value on monitor
10 }while(ch != EOF); //condition to check EOF is reached or not
11 fclose(fp); //free up the file pointer pointed by fp
12 }
13
Write a C program to copy a given file.
Program
1 #include <stdio.h>
2 void main()
3 {
4 FILE *fp1, *fp2; //p and q is a FILE type pointer
5 char ch; //ch is used to store temporary data
6 fp1 = fopen("file1.c","r"); //open file “file1.c” in read mode
7 fp2 = fopen("file2.c","w"); //open file “file2.c” in write mode
8 do { //repeat step 9 and 10 until EOF is reached
9 ch = getc(fp1); //get character pointed by p into ch
10 putc(ch, fp2); //print ch value into file, pointed by pointer q
11 }while(ch != EOF); //condition to check EOF is reached or not
12 fclose(fp1); //free up the file pointer p
13 fclose(fp2); //free up the file pointer q
14 printf("File copied successfully...");
15 }
File Positioning Functions
fseek, ftell, and rewind functions will set the file pointer to new location.
A subsequent read or write will access data from the new position.
Syntax Description
fseek(FILE *fp, fseek() function is used to move the file position to a desired location within the file.
long offset, fp is a FILE pointer, offset is a value of datatype long, and position is an
int position); integer number.
Example: /* Go to the end of the file, past the last character of the file */
fseek(fp, 0L, 2);
long ftell(FILE *fp); ftell takes a file pointer and returns a number of datatype long, that corresponds to
the current position. This function is useful in saving the current position of a file.
Syntax Description
rewind(fp); rewind() takes a file pointer and resets the position to the start of the file.
Example: /* The statement would assign 0 to n because the file position has been set to the
start of the file by rewind. */
rewind(fp);
Write a C program to count lines, words, tabs, and characters
Thank you