This tutorial will teach you all about C file system (file handling in C) from very basic to advance. Let's starts with streams and files.
Streams and Files
Before going to discuss about C file system, it is necessary to know about the term streams and files or the difference between streams and files. C I/O system supplies a consistent interface to the programmer independent of the actual device being accessed i.e., the C I/O system provides a level of abstraction between the programmer and the device and this abstraction is called as stream, and the actual device is called as file. Let's discuss about streams.
Streams
The C file system is designed to work with a large variety of devices, including terminals, disk drives, and tape drives. However each devices is very different, the buffered file system transforms each into a logical device called a stream. There are the following two types of streams:
- Text Streams - A text stream is a sequence of characters.
- Binary Streams - A binary stream is a sequence of bytes that has a one-to-one correspondence to the bytes in the external device, i.e., no character translation occurs.
Files
In C language, a file may be anything from a disk file to a terminal or printer. You can associate a stream with a specific file by performing an open operation. And once a file is opened, information can be exchanged between it and your program.
C File System Basics
The C file system is composed of several interrelated functions. Here the following table lists the most common of these functions. These functions require the header file <stdio.h>
Function Name |
Use |
fopen() |
Opens a file |
fclose() |
Closes a file |
putc() |
Writes a character to a
file |
fputc() |
Same as putc() |
getc() |
Reads a character from a
file |
fgetc() |
Same as getc() |
fgets() |
Reads a string from a
file |
fputs() |
Writes a string to a file |
fseek() |
Seeks to a specified byte
in a file |
ftell() |
Returns the current file
position |
fprintf() |
Is to a file what
printf() is to the console |
fscanf() |
Is to a file what scanf()
is to the console |
feof() |
Returns true if
end-of-file is reached |
ferror() |
Returns true if an error
has occurred |
rewind() |
Resets the file position
indicator to the beginning of the file |
remove() |
Erases a file |
fflush() |
Flushes a file |
The File Pointer
The file pointer is simply the common thread that unites the C I/O system. A file pointer is a pointer to a structure of type FILE. it points to the information that defines various things about the file, including its name, status, and the current position of the file. Here is the statement to use the file pointers in your C program:
FILE *fp;
Opening a File in CTo open a file in C, use fopen() function. The fopen() function opens a stream for use and links a file with that stream. And then it returns the file pointer associated with that file. Here is the prototype of the function fopen()
FILE *fopen(const char *filename, const char *mode)
Here, the filename is the name of the file and mode is the file access mode. Here is the list of all access mode that you can use in opening your file:
Mode |
Use |
r |
Open an existing text
file for reading |
w |
Create a text file for
writing |
a |
Append to a text file |
rb |
Open a binary file for
reading |
wb |
Create a binary file for
writing |
ab |
Append to a binary file |
r+ |
Open a text file for
reading/writing |
w+ |
Create a text file for
reading/writing |
a+ |
Append or create a text
file for reading/writing |
r+b |
Open a binary file for
reading/writing |
w+b |
Create a binary file for
reading/writing |
a+b |
Append or create a binary
file for reading/writing |
Here the following code fragment uses fopen() function to open a file named myfile.txt for reading
FILE *fp;
fp = fopen("myfile.txt", "r");
Closing a File in C
To close a file in C, use fclose() function. This function closes a stream that was opened by a call to fopen(). Here is the prototype of the fclose() function
int fclose(FILE *fp);
Here, fp is the file pointer returned by the call to the function fopen( ).
Writing a Character to a File in C
The C I/O system defines the following two functions that is used in outputting a character to a file. Both functions are equivalent.
putc( )
fputc( )
Here is the prototype of the function putc( )
int putc(int ch, FILE *fp);
Here fp is the file pointer returned by the function fopen(), and ch is the character to be output. If a putc( ) operation is successful, it returns the character written. Otherwise, it returns EOF (end-of-file).
Reading a Character from a File in C
The C I/O system defines the following two functions that is used in inputting a character from a file. Both the functions are equivalent.
getc( )
fgetc( )
Here is the prototype of the function getc( )
int getc(FILE *fp);
Here fp is the file pointer of type FILE returned by fopen( ) function. The function getc( ) returns an EOF when the end of the file has been reached.
C File I/O Examples
Now let's take some examples of C File I/O.
C Reading a File Example
This program reads a file entered by the user and displays the content of the file on the output screen. Before running this program, first create any file say "myfile.txt" and writes some content in the file. Here we have written the following content inside this file:
Single_core_developers
Now concentrate on the program given below.
/* C File I/O - Reading a File in C*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main( )
{
FILE *fp;
char ch, fname[20];
clrscr( );
printf("Enter filename with extension: ");
gets(fname);
fp = fopen(fname, "r");
if(!fp)
{
printf("Error in opening the file..!!");
exit(1);
}
ch = getc(fp); //read one character
while(ch != EOF)
{
putchar(ch); //print on the screen
ch = getc(fp); //read one character
}
fclose(fp); //close file after use
getch( );
}
IF you are using Turbo C/C++ Compiler then always use clrscr( ); and getch( ); .In other compiler you can ignore it.
0 Comments
If you are a good learner please leave a challenging question on comment box for us when you visited this website ( Coding with Fun).