0% found this document useful (0 votes)
8 views15 pages

Working With Files

The document provides an overview of data files in computer systems, detailing their types, file handling processes, and operations in the C programming language. It explains the classification of files into high-level and low-level types, as well as the methods for accessing files, including sequential and random access. Additionally, it covers functions for opening, reading, writing, and manipulating files, along with error handling and examples of file operations.

Uploaded by

x2x9pg4r6r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Working With Files

The document provides an overview of data files in computer systems, detailing their types, file handling processes, and operations in the C programming language. It explains the classification of files into high-level and low-level types, as well as the methods for accessing files, including sequential and random access. Additionally, it covers functions for opening, reading, writing, and manipulating files, along with error handling and examples of file operations.

Uploaded by

x2x9pg4r6r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Files

• A data file is a computer file which stores data to be used by a computer application or
system, including input and output data.
• The data and information are stored in the storage device in the form of file.
• Every file in computer is assigned a unique name called file name with extension.
• File handling or file processing is the process to create a data file, write data to the data
file, read data from the specified data file and edit the records that are present in the file.
File handling consists of three steps or procedures.
Step 1: File has to be opened in order to perform tasks. User also have to create the file, if it
is not created.
Step 2 : After opening the file user either writes data in the file or read data from the file.
Step 3: After all the work is finished, the file has to be closed.
Data files in C are broadly classified to two types :
1) High-Level files(Standard files or stream oriented files)
2) Low-Level files(System oriented files)
1) Standard files: These are often called high level data files. Standard files are of two
types:
i) Text files: Text files comprise consecutive character. These characters can be interpreted
as individual data items or as component of strings. It is created with .txt extension.
ii) Binary files: A binary file stores information in the form of the binary number(0’s and
1’s) and hence occupies less memory space. It is created with a .bin extension. Since it is
not readable to humans, the information is more secure.
2) System Oriented data files : They are more closely related to the computer operating
system. These are often called low level data files.
Types of File
Computer files are categorized into two types:
a) Program File: The file which consists of set of instructions written by using programming
language in order to process data stored in the file is called program file. It consists of source
program file and executable files.
b) Data files: The file which contains different data such as name, address, age, phone etc.
required for data processing is called data files. Data in data files are stored in table form of
rows and column. Data files is processed and managed with the help of program files.
C support two type of files organization or file access methods. They are
a) Sequential access data file
b) Random access data file
Sequential Access Random Access
Sequential access to a file means that computer system Random access to a file means that the computer system
reads or write information to the file sequentially, can read or write information anywhere in the data file.
starting from the beginning of the file and going on step
by step
The file is accessed slowly. The file is accessed quickly.
Sequential file must search through each file. Random files need not to search every files.
There is no predefined order of accessing files. C defines set of function to manipulate the position of
the file
Insertion and updating of file is difficult Insertion and updating is easy
fread(),fscanf(), function is used fseek(), ftell() function is used.
Opening, Reading, Writing and Appending Data
Files

• C languages support number of function to perform various operations such as


defining file, opening file, reading file, appending file and closing file.
fopen()- to create new data file fread()- To read record from data file
fclose() – to close data file fwrite()- to write record to data file
getc() – to read character from a data file
putc() – to write a character to a data file
getw() – to read an integer from data file
putw() – to write an integer to data file
fscanf() – to read formatted data from data file
fprintf() – To write formatted data to data file
Defining and opening Data File

The following syntax is used to define data file


File *fp;
fp=fopen(“filename", "mode”);
The keyword FILE is used to declare file data type. The first statement defines file pointer
variable with data type FILE. The second statement creates file with name filename and
return memory address that is assigned to the file pointer fp. The mode defines the purpose
of the file which can be one of the followings:
r(read) – open an existing file for reading r+ -- open an existing file for both reading
w(write) – open a new file for writing and writing
a – open an existing file for appending w+ -- open a new file for both reading and

a+ - open an existing file for both reading writing.


• Closing Data files : A data file must be closed after doing various operations such as
reading, writing etc. It ensures that all the information associated with the file is
flushed out from the memory and all links to the file are broken. It also helps us to
protects from accidental loss of data from the file. The following syntax is used to
close the file.
fclose(filepointer);
Errors in Opening File- If a file can’t be opened due to various errors, fopen() returns
NULL. So, we can check any error if present by the following codes:
File *fptr;
fptr=fopen(“mydocument.doc”,”w”);
if(fptr==NULL)
{
printf(“Sorry!! File can’t be opened”);
exit(1);
}
Errors may be due to following reasons
a) if we try to open file in read mode and file doesn’t exist
b) if we try to create a file that already exists
c) if there is insufficient space on disk or we don’t have permission to write on the
disk.
Example
#include<stdio.h>
int main()
{
FILE *fptr;
fptr=fopen("data.txt","w");
if(fptr==NULL)
{
printf("\n File cannot be created");
}else
{
printf("File has been succesfully created");
}
fputs("Welcome to my college",fptr);
fclose(fptr);
return 0;
}
File Manipulation Function

Writing Data to a File- The file write operation can be performed by following functions:
a) fputc: The fputc() function is used to write a single character into file. It outputs a
character to a stream. Syntax :fputc(character,file pointer)
b) putc: The putc() function is a C library function, which is used to write a character to a
file. This function is used to write a single character to the stream as well as advance the
position of the indicator. Syntax: putc(chracter, File pointer)
c) putw: It is used to write integers to the file. Syntax: putw(integer variable, File pointer)
d) fputs: It is used to write a string to the file pointed to by the file pointer. Syntax:
fputs(string, file pointer).
e) fprintf: It is used to write string to the file pointed to by file pointer. The string can
include format specifiers and a list of variables.Syntax:fprintf(filepointer,”format
specifier”,list of variables).
• EOF(End of file) : When the file pointer reaches the end of file, it encounter a
character with ASCII value 26 which represents end of file. EOF is a constant defined
in the header file stdio.h which has its value -1.
Reading Data from a File

The data stored in the file must also be read in order to display the data. The function used to
read data from the file are
a) fgetc: It returns the next character from the file pointed to by the file pointer. When the
end of the file has been reached, the EOF is sent back. Syntax: fgetc(file pointer).
b)getc : It is used to read a character from file. Syntax: getc(file pointer)
c) getw: It is used to read integer value from the file. Syntax: getw(file pointer)
d) fgets: It is used to read a file line by line. Reads data until it either encounters a newline or
exceeds the specified buffer length. Syntax:fgets(char *str, int n, FILE *stream).
str - It is the variable in which the string is going to be stored
• n - It is the maximum length of the string that should be read
• stream - It is the filehandle, from where the string is to be read.
e) fscanf: It is used to read characters from the file and assigns the input to a list of
variables. Syntax: fscanf(file pointer,”format specifiers”,variable list)
Deleting file: Remove function is used to delete the file
Syntax: remove(“file name”)
Rename file: Rename function is used to rename the old file name into the new file
name. syntax: rename(“Old file name”,”New file name”)

You might also like