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

Unit 8

Uploaded by

cpansuriya4262
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)
4 views15 pages

Unit 8

Uploaded by

cpansuriya4262
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/ 15

PROGRAMMING FOR

PROBLEM SOLVING
(BE01000121)
CHAPTER-10
FILE MANAGEMENT
ROADMAP
File Operations: Opening, closing, reading, writing
files.

File Pointers: File pointers and basic file


operations (for C).
INTRODUCTION
• Why file handling in C…?
• There are occasions when a program’s output, after it
has been compiled and run, does not meet our
objectives. In these circumstances, the user would want
to verify the program’s output several times. It takes a
lot of work for any programmer to compile and run the
same program repeatedly.
FILE-HANDLING FUNCTIONS
• Basic file operation performed on a file are opening, reading,
writing, and closing a file.
Syntax Description
fp=fopen(file_ This statement opens the file and assigns an
name, mode); identifier to the FILE type pointer fp.

Example: fp = fopen("printfile.c","r");
fclose(filepoin Closes a file and release the pointer.
ter);
Example: fclose(fp);
fprintf(fp, Here fp is a file pointer associated with a file. The
“control string control string contains items to be printed. The list
”, list); may includes variables, constants and strings.

Example:
fprintf(fp, "%s %d %c", name, age, gender);
FILE-HANDLING FUNCTIONS
Syntax Description
fscanf(fp, Here fp is a file pointer associated with a file. The
“control strin control string contains items to be printed. The list may
g”, includes variables, constants and strings.
list);
Example: fscanf(fp, "%s %d", &item, &qty);
int getc( getc() returns the next character from a file referred by
FILE *fp); fp; it require the FILE pointer to tell from which file. It
returns EOF for end of file or error.

Example: c = getc(fp);
int putc(int c putc() writes or appends the character c to the FILE fp. If
, a putc function is successful, it returns the character
FILE *fp); written, EOF if an error occurs.

Example: putc(c, fp);


FILE-HANDLING FUNCTIONS
Syntax Description
int getw(
getw() reads an integer value from FILE pointer fp and
FILE *pvar);
returns an integer.

Example: i = getw(fp);

putw(int,
FILE *fp); putw writes an integer value read from terminal and are
written to the FILE using fp.

Example: putw(i, fp);

EOF
EOF stands for “End of File”. EOF is an integer defined in
<stdio.h>

Example: while(ch != EOF)


FILE OPENING MODES
Mode Description

Open the file for reading only. If it exists, then the file is
r
opened with the current contents; otherwise an error occurs.
Open the file for writing only. A file with specified name is
w created if the file does not exists. The contents are deleted, if
the file already exists.
Open the file for appending (or adding data at the end of file)
data to it. The file is opened with the current contents safe. A
a
file with the specified name is created if the file does not
exists.
The existing file is opened to the beginning for both reading
r+
and writing.
w+ Same as w except both for reading and writing.
a+ Same as a except both for reading and writing.
FILE OPENING MODES

Note: The main difference is w+ truncate


the file to zero length if it exists or create
a new file if it doesn't. While r+ neither
deletes the content nor create a new file if
it doesn't exist.
WRITE A C PROGRAM TO DISPLAY CONTENT OF
A GIVEN FILE.
WRITE A C PROGRAM TO COPY A GIVEN FILE.

• S
FILE POSITIONING FUNCTIONS
• fseek, ftell, and rewind functions will set the file pointer to
new location.
• A subsequent read or write will access data from the new
position.
Syntax Description
fseek(FILE *fp, fseek() function is used to move the file position to
a desired location within the file. fp is a FILE
long offset, pointer, offset is a value of datatype long, and
int position); position is an integer number.

Example: /* Go to the end of the file, past the last character


of the file */
fseek(fp, 0L, 2);
FILE POSITIONING FUNCTIONS
Syntax Description
long ftell(FILE ftell takes a file pointer and returns a number of
*fp); datatype long, that corresponds to the current
position. This function is useful in saving the
current position of a file.

Example: /* n would give the relative offset of the


current position. */
n = ftell(fp);
rewind(fp); rewind() takes a file pointer and resets the position
to the start of the file.

Example: /* The statement would assign 0 to n


because the file position has been set to the start of
the file by rewind. */

rewind(fp);
WRITE A C PROGRAM TO COUNT LINES, WORDS,
TABS, AND CHARACTERS

• S

You might also like