What is a file in C programming ? Explain the use of fopen function in file handling. Explain different mode in which a file can be opened.
What is a file in C programming? Explain the use of fopen function in file handling. Explain different modes in which a file can be opened.
File Input and Output
Overview
A computer file is a computer resource for recording data discretely in a computer storage device. Just as words can be written to paper, so can information be written to a computer file.
There are different types of computer files, designed for different purposes. A file may be designed to store a picture, a written message, a video, a computer program, or a wide variety of other kinds of data. Some types of files can store several types of information at once.
By using computer programs, a person can open, read, change, and close a computer file. Computer files may be reopened, modified, and copied an arbitrary number of times.
Discussion
In computer programming, standard streams are pre-connected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin – keyboard), standard output (stdout – originally a printer), and standard error (stderr – monitor). Streams may be redirected to other devices and/or files. In current environments, stdout is usually redirected to the monitor.
Computer files are stored on secondary storage devices and used to maintain program data over time. Most programming languages have built-in functions or libraries to support processing files as text streams. We need to understand how to open, read, write, and close text files. The following File Input/Output terms are explained:
Text File – A file consisting of characters from the ASCII character code set. Text files (also known as ASCII text files) contain character data. When we create a text file we usually think of it consisting of a series of lines. On each line are several characters (including spaces, punctuation, etc.) and we generally end the line with a return (a character within the ASCII character code set). The return is also known as the new line character. You are most likely already familiar with the escape code of \n which is used within many programming languages to indicate a return character when used within a literal string.
Filename – The name and its extension. Most operating systems have restrictions on which characters can be used in filenames. Example Lab_05.txt
Because some operating systems do not allow spaces, we suggest that you use the underscore where needed for spacing in a filename.
Path (Filespec) – The location of a file along with its filename. filespec is short for the file specifications. Most operating systems have a set of rules on how to specify the drive and directory (or path through several directory levels) along with the filename. Example: C:\myfiles\cosc_1436\Lab_05.txt
Because some operating systems do not allow spaces, we suggest that you use the underscore was needed when creating folders or sub-directories.
Open – Your program requesting the operating system to let it have access to an existing file or to open a new file. In most current programming languages, a file data type exists and is used for file processing. A file variable will be used to store the device token that the operating system assigns to the file being opened. An open function or method is used to retrieve the device token, and typically requires at least two parameters: the path and the mode (read, write, append, or a combination thereof). Corresponding pseudocode would be:
Declare File datafile
datafile = open(filespec, mode)
The open function provides a return value of a device token from the operating system and it is stored in the variable named data.
Read – Moving data from a device that has been opened into a memory location defined in your program. For example:text = read(datafile)
ortext = datafile.read()
Write – Moving data from a memory location defined in your program to a device that has been opened. For example:write(datafile, text)
ordatafile.write(text)
Close – Your program requesting the operating system to release a file that was previously opened. There are two reasons to close a file. First, it releases the file and frees up the associated operating system resources. Second, if closing a file that was opened for output; it will clear out the operating system’s buffer and ensure that all of the data is physically stored in the output file. For example:
close(datafile)
ordatafile.close()
Using / With – A wrapper around a processing block that will automatically close opened resources, available in some programming languages. For example:
// C#
using (datafile = open(filespec, mode))
{
//...
}
or
# Python3 with open(filespec, mode) as datafile: # ...
Key Terms
- close
- Your program requesting the operating system to release a file that was previously opened.
- device token
- A key value provided by the operating system to associate a device to your program.
- filename
- The name and its extension.
- filespec
- The location of a file along with its filename.
- open
- Your program requesting the operating system to let it have access to an existing file or to open a new file.
- read
- Moving data from a device that has been opened into a memory location defined in your program.
- stream
- A sequence of data elements made available over time.
- stdin
- Standard input stream, typically the keyboard.
- stderr
- Standard output error stream, typically the monitor.
- stdout
- Standard output stream, originally a printer, but now typically the monitor.
- text file
- A file consisting of characters from the ASCII character code set.
- using / with
- A wrapper around a processing block that will automatically close open resources.
- write
- Moving data from a memory location defined in your program to a device that has been opened.
- fopen();-
fopen() Declaration: FILE *fopen (const char *filename, const char *mode) fopen() function is used to open a file to perform operations such as reading, writing, etc. In a C program, we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned filename does not exist.
FILE *fp;
fp=fopen (“filename”, ”‘mode”);Where,
fp – file pointer to the data type “FILE”.
filename – the actual file name with the full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+, and a+. Please refer to the description of these modes of operations.
File Opening Mode Chart :
Mode | Meaning | fopen Returns if FILE- | |
---|---|---|---|
Exists | Not Exists | ||
r | Reading | – | NULL |
w | Writing | Overwrite on Existing | Create a New File |
a | Append | – | Create a New File |
r+ | Reading + Writing | New data is written at the beginning overwriting existing data | Create a New File |
w+ | Reading + Writing | Overwrite on Existing | Create a New File |
a+ | Reading + Appending | New data is appended at the end of the file | Create a New File |
What do you think?
I hope, now you have a better understanding of files. Comments and suggestions regarding this article are welcome.
Comments
Post a Comment