0% found this document useful (0 votes)
15 views17 pages

CO-filehandling Notes

The document discusses why files are needed when programming, the different types of files, and common file operations in C like creating, opening, reading and writing to files. It explains how to open files in different modes and what that means for file creation and overwriting.

Uploaded by

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

CO-filehandling Notes

The document discusses why files are needed when programming, the different types of files, and common file operations in C like creating, opening, reading and writing to files. It explains how to open files in different modes and what that means for file creation and overwriting.

Uploaded by

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

sesho) Shar

sert Pormat Sde Arrange Tocls Help


1 ackground Layout Theme transition

Why files are needed?

When a program is terminated, the entire data is lost. Storing in a fle


will preserve your data even if the program terminates.
lot of time to
"If you have to enter a large number of data, it will ta ke a
enter them all.
"However, ifyou have a file containing all the data, youcan easily
access the contents of the file using a fewcommands in C.
another without
Youcan easily move your data from one computer to
any changes.
docs.google.com/presentation/d/1b06n6yilt6qTiNPDqNLPWRewGKQgCYIV/edit#slide=id.p3 Incognito 2

WhatsApp PDF) DOWNLOAD... Kimetsu no Yaiba E... 9 Tokyo Ghoul Ep. 1-... 9 AOT-OVA Tolkyo Ghoul(Sub)

nsert Format Side Arrange Tools Help


Sieeshom) & Share

a- 19Background Layout Theme Transition

Types of Files
When dealing with files, there are two types of files you should knoww about:
Text files
Binary files
1. Text files
Text files are the normal .bt files. Can easily create text files using any simple text editors such as Notepad.
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 the least security and takes bigger storage
space.
2. Binary files
Binary files are mostly the .bin fhles 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 a higher amount of data, are not readable easily, and provides better security than texct files.
file handling insert Format Sicde Arrange Tocls Help Sideshow)s Share
File dt V
- Background Layout Theme Transition

2
File Operations

"In C, you can perform four major operations on files, either text or
binary:
"Creating a new file
"Opening an existing file
"Closinga file
"Reading from and writing information to a file
bGoogle Slides X
docs.google.com/presentation/d/1b06n6yilt6q TiNPDqNLPWRewGKQgCY1V/edit#slide=id.p5 Incognito (2)

Tokyo Ghoul Ep. 1-.. AOT-OVA Tolyo Ghoul (Sub).


WhatsApp 6 PDFJ DOWNLOAD... Kimetsu no Yaiba E..

Siceshow & Share

insert Format Side Arrange Tocls Help


Background Layout Theme Trangition

Working with files


of type file.
"When working with files, you need to declare a pointer the file and
Thisdeclaration is needed for communication between
the program.
"FILE *fptr;
xGo0gle Slides
Incognito (2)
docs.google.com/presentation/d/1 b06n6yilt6qTiNPDqNLPWRewGKQgCYIV/edit#slide=id.p6
Tolkyo Ghoul Ep. 1-.. P AOT-OVA Tolyo Ghoul (Sub)...
WhatsApp PDF] DOWNLOAD... Kimetsu no Yaiba E.
Sliceshow Share
Tools Help
insert Format Slide Arrange
1 8ackground Layout Theme Transition
a

Opening a file - for creation and edit


function defined in
Opening a file is performed using the fopen()
the stdio.h header file.
i/0 is:
The syntax for opening a file in standard
ptr =fopen("fileopen","mode"):
For example,
fopen("E:\\cprogram\\newprogram.txt","w"):
fopen("E:\\cprogram\\oldprogram.bin","rb");
fie handliig Sideshew
insert Format Slide Arrarge Tocls Help
Fle dt Ve
1 8ackground Layout Theme transition

Opening Modes in Standard VO


Mode Meaning of Mode During Inexistence of file

IF the fle does not excist, fopen


Open for reading. returns NULL.

If the file does not exist, fopen)


Open for reading in binary mode. returns NULL.

If the file exists, its contents are


ovenwritten.
Open for writing. If the file does not exist, it will be
created.

If the fle exists, its contents are


overwritten.
wb Open for writing in binary mode. I the file does not exist, it will be
created.
file handling PTX & Share
Sideshow
File Edt i Isert Format Side Arrange Tocls Help

- Background Layout Theme Transition

Open for append. If the fle does not exist, it will be


Data is added to the end of the file. created.

Open for append in binary mode. If the file does not exist, it will be
ab Data is added to the end of the file. created.

If the fle does not exist, fopen)


Open for both reading and writing. returns NULL

Open for both reading and writing in If the file does not exdst, fopen)
rb+ returns NULL.
binary mode.

If the file exists, its contents are


overwritten.
Open for both reading and writing. If the fle does not excist, it will be
created.
docs.google.com/presentation/d/1 b06n6yilt6qTiNPDqNLPWRewGKQgCY1V/edit#slide=id.p9 a D Incognito(2)
S Huawei Rectil WhatsApp (PDF) DOWNLOAD.. Kimetsu no Yaiba E.. Tokyo Ghoul Ep. 1-... 9 AOT-OVA Tolyo Ghoul (Sub)..
file handling PTX
File Edit Vi insert Format Side Arrange Tocls Help Sidesho
Background Layout Theme Transition

If the file exists, its


Wb+ Open for both reading and contents are overwritten.
writing in binary mode. If the file does not exist, it
willbe created.

at Open for both reading and If the file does not exist, it
appending. will be created.

ab+ Open for both reading and If the file does not exist, it
appending in binary mode. will be created.
Siceshow ) Share
Insert Format Side Arrange Tools Help

a- 1 l8ackground| Layout| Theme transition

Closing a File
"The file (both text and binary) should be closed after reading/writing.
"Closing afile is performed using the fclose() function.
"fclose(fptr); Here, fptr is a file pointer associated with the file to be
closed.
Hocs.WhatsApp
9 google.com/presentaDOWNLOAD.
[POF) tion/d/1b06n6yi
.
lt6q TiNPDqNLPWRewGKQgCYIV/edt#slide-=id.pt1
Kimetsu no Yaiba E... S Tokyo
Ghoul Ep.1-... TY
Incognito (2)
AOT-OVA Tolkyo Ghoul (Sub)..
ert Format Side Arrange
Tools Help
sliceshow 3 Share
1 Background Layout Theme ransition

Reading and writing to a text file


"For reading and writingto a text file, we use the
functions fprintf() and fscanf().
"They are just the file versions of printf(l) and scanf().
difference is that fprint() and fscanf() expects a The only
structure FILE. pointer to the
Sideshow S Share
sert Format Slide Arrange Tools Help

a- Gackground | Layout | Theme Tranition

1 Exanple 1: Write to a text file


2 #include <stdio. h>
3 #include cstdlib. h>
Click 4
S int main()
6
7 int num;
8 FILE *fptr;
Linux
1l use appropriate location if you are using Wac0s or
fptr = fopen("C:\\progran. txt",w):
12
3 if(fptr NULL)

15 printf("Error ! "):
exit(1):

printf(Enter num: "):


scanf(",&nun):

fprintf( fptr."xd,nun):
fclase(fptr):
24
25 return 0;
26 )
docs.google.com/presentation/d/1 b06nóyilt6qliNPDqNLPWRewGKQgCYIV/edit#side=id.p13
(PDF] DOWNLOAD... Kimetsu no Yaiba E... Tolyo Ghoul Ep. 1-... P AOT-OVA Tokyo Ghoul (Sub).
WhatsApp
Slideshow & Share

insert Format Side Arrange Tocls Help

al 1 ackground Layout Theme Transition

1 #include <stdio. h>


2 #include <stdlib. h>

Click te: 4 int main()


5-{
6 int num;
7 FILE *fptr:
Click to a NULL)
8 fopen("C:\\program. txt","r")) as
9 if ((fptr = file"):
10 printf("Error! opening
ter re turns NULL.
11
Program exits if the file poin
12 II
exit(1) :
14
15 &num):
16 fscanf( fptr, "%d",
nsd", num) ;
printf("Value of
fclose( fptr):
20
return 0;
21
22 )
9WhatsApp
sert Format Side
a
POF

Ntaion/dMb06nóyilt6qTiNPDqNLPWRevwGKQgCY1V/edit#slide=1d.p14
DOWNLOAD..
Arrange Tocls Help
1
Kimetsu no Yaiba E...

8ackground |Layout Theme Transition


Tokyo Ghoul Ep. 1-..Y
AOT-0VA
Incognito 2
Tolkyo Ghoul(Sub)..
Sleeshow & Share

Reading and writing to a binary file


"Functions
on the diskfread() and fwrite() are used for reading from
and writing to a file
respectively in case of binary files.
"Writing toa binary file
"To write into a binary file, you need to use the
functions take four arguments: fwrite() function. The
"address of data to be written in the disk
" size of data to be written in the disk
"number of such type of data
"pointer to the file where you want to write.
"fwrite(addressData, sizeData, numbersData, pointerToFile);
file handling LOAD..
File Edit Vie
PTX
insert Format Slide
Arrange Tocls Help
S Kimetsu no Yaiba E.. 1V/edit#slide=id.p15AOT-OVA
Tokyo Ghoul Ep. 1-.. V
Q
Incognto 2)
a Tolkyo Ghoul (Sub)..
18ackground Layout Theme Transition Siceshow & Share

Example 3: Write to
#include cstdio.b a binary file using furite()
Binclude <stdlib.h
struct threetum
int nt., n2, n3:
int maln()

int n:
struct threelum nun:
FILE fptr:
1f CCfptr
12 fopen("C:\\program, bin", "wb))
printf("Error1 opening file"): ULL)
1 Program exits if the file pointer returns NULL.
exit(1):

for(n 1; ns 5; **n)
num.nn:
num,n2 $n:
num.n3 5n 1;
fwrite( &nun, sizeof(struct threetum). 1, fptr):
felose(fptr):
PTX Siceshow &Share
fle handling
insert Format Slde Arrange Tools Help
File Edt Vi
18ackground Layout Theme Transition

Example 4: Read froma binary fileusing fread)


8include sstdio.h
8include <stdlib.h
struct threeNum

int n1, n2, n3:

.12 int main()

int n;
struct threeun nun;
13 FILE *fptr:

1f (tfptr fopen(C:Vprogram.bin", "rb")) NULL)


print f("Error! opening file" ):
1 Peogra exts Lf the fale poLnter returns NULL
exit(1):

for (n 1: n< 5: n)
fread(&nun, sizeof(struct threetlun), 1. fptr):
printf(at: 3d\tn2: ita3: d, num,nl, num,n2, num.n3):
fclose( fptr):
return 0:
DOWNLOAD.. 6 Kimetsu no Yaiba E. edtestde=id.p17
Tokyo Ghoul Ep. 1-.. Incognito (2
Insert Format Side Arrange Tocls
Help
AOT-OVA Tolkyo Ghoul (Sub).
G
ackground Layout Theme Transition
Sliceshow D 8 Share

Getting data using fseek()


"If you have many records inside a file
and need to access a record at a
specific position, you need to loop through all the records before it to
get the record.
"This willwaste a lot of memory and operation time. An easier way to
get to therequired data can be achieved using fseek().
"Asthe name suggests, fseek() seeks the cursor to the given record in
the file.
PTX
nsert Format Side
S Kimetsu no Yaiba E.. Jedit#slide=iVd.p18
Tokyo Ghoul Ep. 1-... GIncognito(2
Arrange Tocls Help AOT-OVA Tolkyo Ghou (Sub).
Layout Theme Transition Slideshow 3 Share

Syntax of fseek()
fseek(FlLE
The first
*streanm, long int offset, int
whence);
parameter stream is the pointer to the file. The second
the recordto be found, and the parameter is the position of
third parameter specifies the location where the offset starts.

Different whence in fseek)


Whence Meaning

SEEK
SET Starts the offset from the
beginning of the file.
Starts the offset from the end of
SEEK_END the file.

Starts the offset from the current


SEEK_CUR location of the cursor in the file.

You might also like