{"id":14664,"date":"2024-02-06T17:00:02","date_gmt":"2024-02-06T11:30:02","guid":{"rendered":"https:\/\/btechgeeks.com\/?p=14664"},"modified":"2024-02-07T17:12:01","modified_gmt":"2024-02-07T11:42:01","slug":"c-programming-file-handling-input-output","status":"publish","type":"post","link":"https:\/\/btechgeeks.com\/c-programming-file-handling-input-output\/","title":{"rendered":"File Input Output in C Programming"},"content":{"rendered":"
File in C programming language, can be anything from a disk file to a device. C language provide support for opening a file, closing a file, reading data from a file and writing data to a file through a set of standard library functions.<\/p>\n
C file handling is designed to work with a number of devices and file formats like printer, screen, keyboard, disk file etc. C file system transforms different devices into a abstract logical device called stream. All streams behaves same irrespective of devices and files. It provides an abstract consistent interface for C program to interact by hiding the details of the device specific implementations. Disk file read function can be used to read data from keyboard as well.<\/p>\n
Points to Remember about Streams<\/strong><\/p>\n
\n
A Stream is a the sequence of bytes of data.<\/li>\n
All Streams behaves similarly. Same code can be used to perform I\/O operations of various streams.<\/li>\n
A stream hides the low level device dependent complexities from C program.<\/li>\n
In a C program, all input and Output is done through streams.<\/li>\n
Three streams gets automatically attached when a program starts execution that is<\/li>\n
Standard Input Stream(stdin)<\/li>\n
Standard Output Stream(stdout)<\/li>\n
Standard Error Stream(stderr)<\/b><\/li>\n
Keyboard is associated with stdin stream.<\/li>\n
Screen is associated with stdout stream.<\/li>\n<\/ul>\n
FILE Pointer in C<\/h3>\n
A file pointer is a pointer to a structure of type FILE. A file pointer is associated with stream and manages all Input and Output operations of that stream. The FILE structure contains information which is required to perform any file I\/O operations like name of the file, current location of position indicator in file, data transfer bugger, status flag etc.<\/p>\n
A file pointer is a bridge between c program and file\/stream. We can declare a\u00a0FILE pointer<\/b>\u00a0as follows:<\/p>\n
FILE *file;<\/em><\/strong> \nFacts about FILE Structure<\/strong><\/p>\n
\n
To perform any I\/O operation on file, we need FILE pointer.<\/li>\n
FILE Structure is defined in stdio.h header file.<\/li>\n
FILE Structure contains necessary information about a stream, which is required to perform I\/O operation in that stream.<\/li>\n
FILE pointer is an interface for our programs to interact with files.<\/li>\n<\/ul>\n
Opening a File in C<\/h3>\n
The stdio.h library function fopen() is used to create a new file or open an exiting file.<\/p>\n
Function fopen() opens the file whose name is given in the filename argument and associate it with a stream and return a FILE pointer to be used in any future I\/O operations on this stream. The fopen function opens a stream in a particular mode, which defines the operations that are allowed on the stream. \nVarious Modes of Opening a File<\/big><\/p>\n
\n\n
\n
Mode<\/th>\n
Description<\/th>\n<\/tr>\n<\/thead>\n
\n
\n
“r”<\/td>\n
To read a file. Opens a file for reading. The file must exist.<\/td>\n<\/tr>\n
\n
“w”<\/td>\n
To write on a file. Creates an empty file for writing. If a file already exists with same name, its content is removed and the file is considered as a new empty file.<\/td>\n<\/tr>\n
\n
“a”<\/td>\n
To append data at the end of file. The file is created if it does not exist.<\/td>\n<\/tr>\n
\n
“r+”<\/td>\n
To read and write on an existing file. Opens a file to update both reading and writing. The file must exist.<\/td>\n<\/tr>\n
\n
“w+”<\/td>\n
To create a new file for reading and writing.<\/td>\n<\/tr>\n
\n
“a+”<\/td>\n
To read and append data on a file.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
C Program to Open a File using fopen Function<\/h3>\n
The following program shows the use of\u00a0fopen function<\/b>\u00a0to open a text file in read mode.<\/p>\n
<\/p>\n
#include <stdio.h>\r\n \r\nint main(){\r\n FILE *file;\r\n int ch;\r\n \r\n \/* Open a file for reading *\/\r\n file = fopen(\"textFile.txt\",\"r\");\r\n if(file == NULL){\r\n perror(\"Error: Unable to open a file\");\r\n } else {\r\n while(!feof(file)){\r\n ch = fgetc(file);\r\n printf(\"%c\", ch);\r\n }\r\n fclose(file);\r\n }\r\n \r\n return(0);\r\n}<\/pre>\n
Output<\/p>\n
fopen C Standard library function\r\n<\/pre>\n
Closing a File in C<\/h3>\n
The stdio.h library function fclose() is used to close a stream that was opened by fopen() function. Before closing a stream it flushes it’s buffer.<\/p>\n