Data Files - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Data Files

Description:

A file can be used as a source of data that needs to be loaded when a program ... functions for checking and resetting errors associated with file operations. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 13
Provided by: anniegro
Category:
Tags: data | files | resetting

less

Transcript and Presenter's Notes

Title: Data Files


1
File I/O
2
Data Files
Many applications prefer to store much of their
data to files. A file can be used as a source
of data that needs to be loaded when a program
begins execution. A file can also be used to
store information that is generated by the
execution of a program. C supports operations on
2 types of files ascii/text and binary. When
working with files, the programmer typically
opens a file, reads/writes from/to the file, then
closes the file. It is important to close a file
as soon as you are finished working with it.
3
Text Files
The stdio.h library file contains a data type
called FILE. The data type is case
sensitive! As a programmer, youll be using file
pointers FILE infileptr FILE outfileptr
The following library functions are used for
manipulating text file pointers fopen opens a
file fclose closes a file fscanf reads from
a file fprintf writes to a file fputc/putc
puts a character to a file fgetc/getc gets a
character from a file fgets gets a string of
characters from a file fputs writes a string of
characters to a file
4
Text File Operations
To open a file, provide the filename and opening
properties to fopen infileptr
fopen(input.txt,r) outfileptr
fopen(output.txt, w) The name of the file
is represented as a string containing the
absolute path to the file. The opening
properties can be one of the following strings
optionally followed by a b r open for
reading w open for writing a append open
or create file for writing at the end r open
file for update reading and writing w
create file for update discard previous contents
if any a append open or create file for
update, writing at end
5
If the file cannot be opened, fopen returns a
NULL pointer. Remember to check for this /
formatted file I/O / fscanf(infileptr, d,
num) fprintf(outfileptr, Number d,
num) / character I/O / ch
fgetc(infileptr) fputc(ch, outfileptr) /
string I/O / fgets(my_str, 80,
infileptr) fputs(my_str, outfileptr) / close
the files / fclose(infileptr) fclose(outfileptr)

6
Binary Files
A binary file can be specified using the
character b in the opening properties
string. Binary files have the advantage of
requiring much less space than text files. They
have the disadvantage of not being readable by
the human eye. stdio library functions for
manipulating binary files include fwrite fread
7
Binary File Operations
  • fwrite takes 4 parameters
  • The address of the first memory cell whose
    contents are to be written to the file
  • The number of bytes to be copied to the file for
    one component.
  • The number of values to write to the file
  • The file pointer
  • int temp 10
  • FILE binfileptr
  • binfileptr fopen(out.bin,wb)
  • fwrite(temp, sizeof(temp), 1, binfileptr)
  • How could you write the contents of an array to a
    binary file?

8
  • The fread function also takes 4 parameters
  • Address of first memory cell to fill
  • Size of one value
  • Maximum number of elements to copy from the file
    into memory
  • File pointer
  • int temp
  • FILE binfileptr
  • binfileptr fopen(out.bin,rb)
  • fread(temp, sizeof(temp), 1, binfileptr)

9
File Positioning
Since a file is handled as a pointer to a
sequence of bytes, you can have random access to
the data. There are several stdio library
functions that provide file positioning
behavior. int fseek(FILE stream, long offset,
int origin) fseek sets the file position for a
stream. Any subsequent reads or writes will use
this position. The offset is relative to the
origin. The origin may be either SEEK_SET(beginn
ing) SEEK_CUR(current position) SEEK_END(end of
file) For text files, the offset may be either 0
or the value returned from ftell.
10
long ftell(FILE stream) ftell returns the
current file position for the input stream. -1L
for an error. int fgetpos(FILE stream, fpos_t
ptr) int fsetpos(FILE stream, const fpos_t
ptr) fgetpos retrieves the current position in
the stream for subsequent calls to
fsetpos. FILE stream stream
fopen(sample.txt, w) fseek(stream, 0L,
SEEK_END) long pos ftell(stream) fseek(stream
, pos, SEEK_SET) fpos_t pos2 if
(!fgetpos(stream, pos2)) fsetpos(stream,
pos2)
11
File Error Functions
There are many possibilities to make errors when
working with files. Some errors are programmer
errors some errors are device related
errors. The stdio library provides functions for
checking and resetting errors associated with
file operations. int feof(FILE stream) Returns
non-zero if the eof indicator is set for stream.
This indicator is set when an attempt has been
made to read past the end of file.
12
int ferror(FILE stream) Returns non-zero if the
error indicator for stream is set. This
indicator would be set in the case of an I/O
device failure. void clearerr(FILE
stream) Clears the end of file and error
indicators for the input stream. These error
functions can be used to validate file reads that
use fscanf. Recall that fscanf returns an
integral value which corresponds to the number pf
inputs that it successfully read and converted.
int data, flag FILE fin fopen(in.txt,
r) flag fscanf(fin, d,data) if (flag
! 1) / an error occurred / if (feof(fin))
/ end of file found / else if (ferror(fin))
/ device failure /
Write a Comment
User Comments (0)
About PowerShow.com