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

Programming in C FILE PROCESSING

The document provides an overview of file processing in C, detailing the importance of files for data storage, types of files (text and binary), and various file operations such as reading, writing, and closing files. It also covers file access methods (sequential and random), along with relevant functions like fopen(), fclose(), fseek(), and ftell(). Additionally, it explains command line arguments and their usage in C programming.

Uploaded by

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

Programming in C FILE PROCESSING

The document provides an overview of file processing in C, detailing the importance of files for data storage, types of files (text and binary), and various file operations such as reading, writing, and closing files. It also covers file access methods (sequential and random), along with relevant functions like fopen(), fclose(), fseek(), and ftell(). Additionally, it explains command line arguments and their usage in C programming.

Uploaded by

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

UNIT V FILE PROCESSING

FILES
 A file represents a sequence of bytes on the disk where a group of related data is stored.
 File is created for permanent storage of data.
 It is a readymade structure.
Why files are needed?

o When a program is terminated, the entire data is lost. Storing in a file will preserve your data even
if the program terminates.
o If you have to enter a large number of data, it will take a lot of time to enter them all.

o However, if you have a file containing all the data, you can easily access the contents of the
file using few commands in C.
o You can easily move your data from one computer to another without any changes.

Types of Files
When dealing with files, there are two types of files you should know about:

1. Text files
2. Binary files
Text files
 Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.
 When you open those files, you'll see all the contents within the file as plain text. You can easily edit
or delete the contents.
 They take minimum effort to maintain, are easily readable, and provide least security and takes
bigger storage space.

1. w(write):
This mode opens new file on the disk for writing.If the file exist,disk for writing.If the file exist, then it will be
over written without then it will be over written without any confirmation.

SYNTAX:
fp=fopen("data.txt","w");
"data.txt" is filename
"w" is write mode.
2. r (read)
This mode opens an preexisting file for reading.If the file doesn‟t Exist then the compiler returns a NULL to the
file pointer
SYNTAX:
fp=fopen("data.txt","r");
3. w+(read and write)
This mode searches for a file if it is found contents are destroyed If the file doesn‟t found a new file is created.
SYNTAX:
fp=fopen("data.txt","w+");
4. a(append)
This mode opens a preexisting file for appending the data.
SYNTAX:
fp=fopen("data.txt",");

5 .a+(append+read)
the end of the file.
SYNTAX:
fp=fopen("data.txt","a+");
6. r+ (read +write)
This mode is used for both Reading and writing

Binary files
 Binary files are mostly the .bin files in your computer.
 Instead of storing data in plain text, they store it in the binary form (0's and 1's).
 They can hold higher amount of data, are not readable easily and provides abetter security than
text files.

1. wb(write)

this opens a binary file in write mode.

SYNTAX:

fp=fopen(“data.dat”,”wb”);

2. rb(read)

this opens a binary file in read mode

SYNTAX:

fp=fopen(“data.dat”,”rb”);

3. ab(append)

this opens a binary file in a Append mode i.e. data can be added at the end of file.

SYNTAX:

fp=fopen(“data.dat”,”ab”);

4. r+b(read+write)

this mode opens preexisting File in read and write mode.

SYNTAX:

fp=fopen(“data.dat”,”r+b”);
5. w+b(write+read)

this mode creates a new file for reading and writing in Binary mode.

SYNTAX:

fp=fopen(“data.dat”,”w+b”);

6. a+b(append+write)

this mode opens a file in append mode i.e. data can be written at the end of file.

SYNTAX:

fp=fopen(“data.dat”,”a+b”);
File Operations
In C, you can perform four major operations on the file, either text or binary:
a. Creating a new file
b. Opening an existing file
c. Closing a file
d. Reading from and writing information to afile
e. C provides a number of functions that helps to perform basic file operations. Following
are the functions,

Function Description
fopen() create a new file or open a existingfile
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from afile
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desirepoint
ftell() gives current position in the file
rewind() set the position to the beginningpoint
Opening a File or Creating a File
Thefopen() function is used to create anew fileorto open an existing file.
Syntax:
*fp = FILE fopen(const char *filename, const char *mode);

 Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened (or created) file.
Filename is the name of the file to be opened and mode specifies the purpose of opening the file. Mode
can be of following types,
Mode Description

r opens a text file in readingmode

w opens or create a text file in writing mode.

a opens a text file in appendmode

r+ opens a text file in both reading and writing

mode w+ opens a text file in both reading and writing

mode a+ opens a text file in both reading and writing

mode rb opens a binary file in reading mode

wb opens or create a binary file in writing

mode ab opens a binary file in append mode

rb+ opens a binary file in both reading and writing

mode wb+ opens a binary file in both reading and

writing mode

ab+ opens a binary file in both reading and writing mode


Closing a File
 The fclose() function is used to close an alreadyopened file.
Syntax:
int fclose( FILE *fp);

 Here fclose() function closes the file and returns zero on success, or EOF if there is an error in
closing the file. This EOF is a constant defined in the header file stdio.h.

Input/ Output operation on File


 In the above table we have discussed about various file I/O functions to perform reading and writing on
file.
 getc() and putc() are the simplest functions which can be used to read and write individual characters to
a file.

Example:
#include<stdio.h>
int main()
{
Output:
Enter data...hello welcome to it department
FILE *fp; char ch; The Contents of file is
fp = fopen("one.txt", "w"); hello welcome to it department
printf("Enter data...");
while( (ch = getchar()) != '\n')
{
putc(ch, fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
printf(“The Contents of file is \n”);
while((ch =fgetc(fp))!= EOF)
{
printf("%c",ch);
}
fclose(fp);
return 0;
}
TYPES OF FILE PROCESSING: SEQUENTIAL ACCESS, RANDOM ACCESS

In computer programming, the two main types of file handling are:

Sequential access
 In this type of files data is kept in sequential order if we want to read the last record of the file, we
need to read all records before that record so it takes more time.
Random access
 In this type of files data can be read and modified randomly .If we want to read the last record we
can read it directly. It takes less time when compared to sequential file.
 There is no need to read each record sequentially, if we want to access a particular
record. C supports these functions for random access file processing.

1. fseek()
2. ftell()
3. rewind()
fseek():
It is used to move the reading control to different positions using fseek function.

Syntax:

fseek( File pointer, Displacement, Pointer position);

Where

File pointer-------It is the pointer which points to the file.


Displacement ------- It is positive or negative. This is the number of bytes which are skipped backward (if
negative) or forward (if positive) from the current position. This is attached with L because this is a long integer.

Pointer position:
This sets the pointer position in the file.

Value Pointer Position

0 Beginning of file
1 Current position
2 End of file

Example:
1) fseek( p,10L,0)
 0 means pointer position is on beginning of the file, from this statement pointer position is skipped
10 bytes from the beginning of thefile.
2) fseek( p,5L,1)
 1 means current position of the pointer position. From this statement pointer position is skipped 5
bytes forward from the current position.
3) fseek(p,-5L,1)
 From this statement pointer position is skipped 5 bytes backward from the current position.
 ftell(): It tells the byte location of current position of cursor in file pointer.
 rewind(): It moves the control to beginning of the file.

ftell()

This function returns the value of the current pointer position in the file.The value is count from the
beginning of the file.

Syntax: ftell(fptr);

Where fptr is a file pointer.


rewind()

This function is used to move the file pointer to the beginning of the given file.

Syntax: rewind( fptr);

Where fptr is a file pointer.

COMMAND LINE ARGUMENTS


 Command line argument is a parameter supplied to the program when it is invoked.
 Command line argument is an important concept in C programming.
 It is mostly used when you need to control your program from outside.
 Command line arguments are passed to the main() method.

Syntax:

int main(int argc, char *argv[])

 Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which
holds pointers of type char which points to the arguments passed to the program.
Example:
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments suppliedare:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
}
else
{
printf("argument list is empty.\n");
}
return 0;
}
Remember that argv[0] holds the name of the program and

argv[1] points to the first command line argument and argv[n] gives the last argument.

If no argument is supplied, argc will be 1.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy