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

week-6

The document outlines the objectives and content for Week 6 of CENG 110, focusing on C file processing with sequential access. It covers concepts of files and streams, methods for reading and writing to files, and details on file open modes. Additionally, it emphasizes the importance of data retention and the handling of errors in file processing.

Uploaded by

batam54516
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

week-6

The document outlines the objectives and content for Week 6 of CENG 110, focusing on C file processing with sequential access. It covers concepts of files and streams, methods for reading and writing to files, and details on file open modes. Additionally, it emphasizes the importance of data retention and the handling of errors in file processing.

Uploaded by

batam54516
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CENG 110 – PROGRAMMING AND

COMPUTING II

Week 6 : C File Processing (Sequential


Access)

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


Objectives
• Understand the concepts of files and streams.
• Write data to and read data from files using sequential-
access text-file processing.
• Write data to, update data in and read data from files
using random-access file processing and binary files.
• Develop a substantial transaction-processing program.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


Outline
11.1 Introduction

11.2 Files and Streams

11.3 Creating a Sequential-Access File


11.3.1 Pointer to a FILE
11.3.2 Using fopen to Open a File
11.3.3 Using feof to Check for the End-of-File Indicator
11.3.4 Using fprintf to Write to a File
11.3.5 Using fclose to Close a File
11.3.6 File-Open Modes

11.4 Reading Data from a Sequential-Access File


11.4.1 Resetting the File Position Pointer
11.4.2 Credit Inquiry Program

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.1 Introduction
• Data in variables is temporary—it’s lost when a program
terminates.
• Files enable long-term data retention
• This chapter explains how to create, update and process
data files.
• We consider both sequential-access and random-access
file processing.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.2 Files and Streams (1 of 3)
• C views each file as a sequential stream of bytes

• Each file ends with an end-of-file marker or at a specific byte number


recorded in a system-maintained, administrative data structure
• This is platform-dependent and hidden from you.
• Standard Streams in Every Program
– The standard input stream receives input from the keyboard.
– The standard output stream displays output on the screen.
– The standard error stream displays error messages on the screen.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.2 Files and Streams (2 of 3)
• FILE Structure
– Opening a file returns a pointer to a FILE structure (defined in
<stdio.h>)
– May include a file descriptor—an integer index into an operating-
system array called the open file table
▪ Each element contains a file control block (FCB)—information
the operating system uses to administer a particular file.
– Manipulate the standard input, standard output and standard error
streams using the FILE pointers stdin, stdout and stderr.
• File-Processing Function fgetc
– Like getchar, but reads one character from the file specified by
its FILE pointer argument

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.2 Files and Streams (3 of 3)
• File-Processing Function fputc
– Like putchar, but writes the character in its first argument
to the file specified by the FILE pointer in its second
argument.
• Other File-Processing Functions
– The fgets and fputs functions read a line of text from a
file and write a line of text to a file, respectively.
– The next few sections introduce the file-processing
equivalents of functions scanf and printf—fscanf
and fprintf.
– Later in the chapter, we discuss functions fread and
fwrite.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
11.3 Creating a Sequential-Access File
(1 of 8)

• C imposes no structure on a file


• Notions such as a record of a file are not part of the C
language
• The following example shows how you can impose your own
record structure on a file

11.3.1 Pointer to a FILE


• A program refers to each open file with a separate FILE
pointer
• You need not know the FILE structure’s specifics to use files

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access File
(2 of 8)

11.3.2 Using fopen to Open a File


• fopen opens a file and establishes a “line of communication” with it

• Two arguments
– a filename (which can include path information leading to the
file’s location)
– a file open mode
• File open mode "w" indicates fopen should open the file for writing
– If the file does not exist, fopen creates the file
– If you open an existing file, fopen discards the file’s contents
without warning
• If the file pointer fopen returns is NULL, the file could not be opened

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access File
(3 of 8)

11.3.3 Using feof to Check for the End-of-File Indicator


• The key combinations for end-of-file are platform-dependent:
– Windows: <Ctrl> + z, then press Enter
– macOS/Linux: <Ctrl> + d
• feof determines whether the end-of-file indicator is set
– End-of-file indicator informs the program that there’s no
more data to process
– The argument is a FILE pointer to the file to test
• Returns a nonzero (true) value when the end-of-file indicator
has been set; otherwise, the function returns zero (false).

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access
File (4 of 8)
11.3.4 Using fprintf to Write to a File
• Equivalent to printf, but fprintf also receives a FILE
pointer argument specifying the file to which the data will be
written
11.3.5 Using fclose to Close a File
• Function fclose receives the FILE pointer as an argument
• If you do not call fclose explicitly, the operating system
normally will close the file when program execution terminates
• Close each file as soon as it’s no longer needed

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access
File (5 of 8)

11.3.6 File-Open Modes

Mode Description
r Open an existing file for reading.
w Create a file for writing. If the file already exists, discard the current
contents.
a Open or create a file for writing at the end of a file—this is for write
operations that append data to a file.
r+ Open an existing file for update (reading and writing).
w+ Create a file for reading and writing. If the file already exists, discard the
current contents.
a+ Open or create a file for reading and updating where all writing is done
at the end of the file—that is, write operations append data to the file.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access
File (6 of 8)
11.3.6 File-Open Modes
• The ones containing the letter "b" are for manipulating binary files

Mode Description
rb Open an existing binary file for reading.
wb Create a binary file for writing. If the file already exists, discard the
current contents.
ab Open or create a binary file for writing at the end of the file
(appending).
rb+ Open an existing binary file for update (reading and writing).
wb+ Create a binary file for update. If the file already exists, discard the
current contents.
ab+ Open or create a binary file for update. Writing is done at the end of
the file.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
11.3 Creating a Sequential-Access
File (7 of 8)

11.3.6 File-Open Modes


• C11 Exclusive Write Mode
– Indicated with "wx", "w+x", "wbx" or "wb+x".
– fopen fails if the file already exists or cannot be
created
– If your program successfully opens a file in exclusive
write mode and the underlying system supports
exclusive file access, then only your program can
access the file while it’s open

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.3 Creating a Sequential-Access
File (8 of 8)

• Common File-Processing Errors


– Opening a nonexistent file for reading.
– Opening a file for reading or writing without having
been granted the appropriate access rights to the file
(this is operating-system dependent).
– Opening a file for writing when no space is available.
– Opening a file in write mode ("w") when it should be
opened in update mode ("r+")—"w" discards the file’s
contents.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.4 Reading Data from a Sequential-
Access File
• Data is stored in files so that it can be retrieved for processing
when needed.
• If a file’s contents should not be modified, open the file only for
reading.
– Prevents unintentional modification of the file’s contents
and is another example of the principle of least privilege.
• Open a file for reading with the "r" mode.
• Function fscanf is equivalent to scanf but receives as its
first argument a FILE pointer for the file from which to read.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.4.1 Resetting the File Position Pointer
• When retrieving data sequentially from a file, a program normally
reads from the beginning of the file until the desired data is found
• In some cases, a program must process a file sequentially several
times from the beginning
• rewind(cfPtr);
– Repositions the file position pointer to the beginning (byte 0) of
the file pointed to by cfPtr
– The file position pointer is not really a pointer
– It’s an integer indicating the byte number of the next byte to read
or write.
– Sometimes referred to as the file offset

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


11.4.2 Credit Inquiry Program
• You cannot modify data in this type of sequential file without the risk of
destroying other data

• For example, if the name "White" needs to be changed to "Worthington", you


cannot simply overwrite the old name

• The new record has more characters than the original record

• The characters beyond the second "o" in "Worthington" will overwrite the
beginning of the next sequential record in the file
• In the formatted input/output model using fprintf and fscanf, fields
and records can vary in size

– 7, 14, – 117, 2074 and 27383 are all ints stored internally in
the same number of bytes, but they’re different sizes when displayed
or written to a file as text

Copyright © 2022 Pearson Education, Inc. All Rights Reserved

You might also like