0% found this document useful (0 votes)
15 views

File Management

The document discusses file management and file handling functions in C programming. It explains what files are, different file opening modes, basic file handling functions like fopen, fclose, fprintf, fscanf. It also discusses file positioning functions like fseek, ftell, rewind and provides examples of programs to display file contents, copy a file, count lines, words, characters in a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

File Management

The document discusses file management and file handling functions in C programming. It explains what files are, different file opening modes, basic file handling functions like fopen, fclose, fprintf, fscanf. It also discusses file positioning functions like fseek, ftell, rewind and provides examples of programs to display file contents, copy a file, count lines, words, characters in a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

File

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

Text File Binary File


Hello, this is a text file. Whatever 1101001101010001011011101010111
written here can be read easily 0101110100110101000101101110101
without the help of a computer. 0111010111010011
File Opening Modes
We can perform different operations on a file based on the file opening modes
Mode Description

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.

w+ Same as w except both for reading and writing.


a+ Same as a except both for 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.

Example: putc(c, fp);


File Handling Functions
Syntax Description
int getw( getw() reads an integer value from FILE pointer fp and returns an integer.
FILE *pvar);
Example: i = getw(fp);
putw(int,
FILE *fp); putw writes an integer value read from terminal and are written to the FILE using fp.

Example: putw(i, fp);

EOF EOF stands for “End of File”. EOF is an integer defined in <stdio.h>

Example: while(ch != EOF)


Write a C program to display content of a given file.

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.

Example: /* n would give the relative offset of the current position. */


n = ftell(fp);
File Positioning Functions

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

Program Program (contd.)


1 #include <stdio.h> 17 c++;
2 void main() 18
3 { 19 ch = getc(p);
4 FILE *p; 20 }
5 char ch; 21 fclose(p);
6 int ln=0,t=0,w=0,c=0; 22 printf("Lines = %d, tabs = %d, w
7 p = fopen("text1.txt","r"); ords = %d, characters = %d\n",ln, t,
8 ch = getc(p); w, c);
9 while (ch != EOF) { 23 }
10 if (ch == '\n')
11 ln++;
12 else if(ch == '\t') Output
13 t++; Lines = 22, tabs = 0, words = 152, characters = 283
14 else if(ch == ' ')
15 w++;
16 else
Practice Programs
1) Write a C program to write a string in file.
2) A file named data contains series of integer numbers. Write a C program to read all
numbers from file and then write all the odd numbers into file named “odd” and
write all even numbers into file named “even”. Display all the contents of these file
on screen.
3) Write a C program to read name and marks of n number of students and store them
in a file.
4) Write a C program to print contents in reverse order of a file.
5) Write a C program to compare contents of two files.
6) Write a C program to copy number of bytes from a specific offset to another file.
7) Write a C program to convert all characters in UPPER CASE of a File.

Thank you

You might also like