0% found this document useful (0 votes)
61 views29 pages

Input and Output Manipulations

Computer files can store different types of data and be manipulated through programs. There are three main steps to using a file in a program: 1) opening the file to create a connection between it and the program, 2) processing the file by reading from or writing to it, and 3) closing the file to disconnect it from the program. Files can be text files containing character data that can be viewed in a text editor, or binary files containing non-text data intended only for programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views29 pages

Input and Output Manipulations

Computer files can store different types of data and be manipulated through programs. There are three main steps to using a file in a program: 1) opening the file to create a connection between it and the program, 2) processing the file by reading from or writing to it, and 3) closing the file to disconnect it from the program. Files can be text files containing character data that can be viewed in a text editor, or binary files containing non-text data intended only for programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

INPUT/OUTPUT FILE

MANIPULATIONS
A computer file is a computer resource for recording data
discretely in a computer storage device. Just as words can be
written to paper, so can information be written to a computer
file.
There are different types of computer files, designed for
different purposes. A file may be designed to store a picture, a
written message, a video, a computer program, or a wide variety
of other kinds of data. Some types of files can store several types
of information at once.
 By using computer programs, a person can open, read, change,
and close a computer file. Computer files may be reopened,
modified, and copied an arbitrary number of times.
 In computer programming, standard streams are pre-connected
input and output communication channels between a computer
program and its environment when it begins execution. The three
input/output (I/O) connections are called standard input (stdin –
keyboard), standard output (stdout – originally a printer) and
standard error (stderr – monitor). Streams may be redirected to
other devices and/or files. In current environments, stdout is
usually redirected to the monitor.
 Computer files are stored on secondary storage devices and used
to maintain program data over time. Most programming
languages have built-in functions or libraries to support
processing files as text streams. We need to understand how to
open, read, write and close text files. 
Text File – A file consisting of characters from the ASCII character
code set. Text files (also known as ASCII text files) contain
character data. When we create a text file we usually think of it
consisting of a series of lines. On each line are several characters
(including spaces, punctuation, etc.) and we generally end the line
with a return (a character within the ASCII character code set). The
return is also known as the new line character. You are most likely
already familiar with the escape code of \n which is used within
many programming languages to indicate a return character when
used within a literal string.
A typical text file consisting of lines can be created by text editors
(Notepad) or word processing programs (Microsoft Word). When
using a word processor you must usually specify the output file as
text (.txt) when saving it. Most source code files are ASCII text files
with a unique file extension; such as C++ using .cpp, C# using .cs,
Python using .py, etc. Thus, most compiler/Integrated
Development Environment software packages can be used to
create ASCII text files.
Filename – The name and its extension. Most operating systems
have restrictions on which characters can be used in filenames.
Example Lab_05.txt
Because some operating systems do not allow spaces, we suggest
that you use the underscore where needed for spacing in a filename.
Path (Filespec) – The location of a file along with its
filename. Filespec is short for file specification. Most
operating systems have a set of rules on how to specify the
drive and directory (or path through several directory
levels) along with the filename. Example: C:\myfiles\
cosc_1436\Lab_05.txt
Because some operating systems do not allow spaces, we
suggest that you use the underscore where needed when
creating folders or sub-directories.
Open – Your program requesting the operating system to let it have
access to an existing file or to open a new file. In most current
programming languages, a file data type exists and is used for file
processing. A file variable will be used to store the device token that
the operating system assigns to the file being opened. An open
function or method is used to retrieve the device token, and
typically requires at least two parameters: the path and the mode
(read, write, append, or a combination thereof). Corresponding
pseudocode would be:
The open function provides a return value of a device token from
the operating system and it is stored in the variable named data.
It is considered good programming practice to determine if the file
was opened properly. The reason the operating system usually can’t
open a file is because the filespec is wrong (misspelled or not typed
case consistent in some operating systems) or the file is not stored
in the location specified. Accessing files stored on a network or the
Internet may fail due to a network error.
Verifying that a file was opened properly is processed with a
condition control structure. That structure may be either be an if-
then-else statement or a try-catch / try-except error handler,
depending on the programming language used.
Introduction to File Input and Output

 When a program needs to save data for later use, it writes the data in a file. The
data can be read from the file at a later time.

 The programs you have written so far require the user to reenter data each time
the program runs, because data that is stored in RAM (referenced by variables)
disappears once the program stops running. If a program is to retain data between
the times it runs, it must have a way of saving it. Data is saved in a file, which is
usually stored on a computer’s connected disk drives. Once the data is saved in a
file, it will remain there after the program stops running or the computer is shut
off. Data that is stored in a file can be retrieved and used at a later time.

 Commercial software packages store data in files.


Here are a few examples.

Word processors. Word processing programs are used to write letters, memos, reports,
and other types of documents. The documents are then saved in files so they can be
edited and printed.

Image/ Graphic editors. Image editing programs are used to draw graphics and edit
images such as the ones that you take with a digital camera. The images that you
create or edit with an image editor are saved in files.
Spreadsheets. Spreadsheet programs are used to work with numerical data. Numbers
and mathematical formulas can be inserted into the rows and columns of the
spreadsheet. The spreadsheet can then be saved in a file for use later.

Games. Many computer games keep data stored in files. For example, some games
keep a list of player names with their scores stored in a file. These games typically
display the players’ names in order of their scores, from highest to lowest. Some
games also allow you to save your current game status in a file so you can quit the
game and then resume playing it later without having to start from the beginning.
 Programs that are used in daily business operations rely extensively on files.
Payroll programs keep employee data in files, inventory programs keep
data about a company’s products in files, accounting systems keep data
about a company’s financial operations in files, and so on. Programmers
usually refer to the process of saving data in a file as “writing data to” the
file. When a piece of data is written to a file, it is copied from a variable in
RAM to the file.

 The process of retrieving data from a file is known as “reading data from”
the file. When a piece of data is read from a file, it is copied from the file
into RAM and referenced by a variable.

 The term input file is used to describe a file that data is read from. It is
called an input file because the program gets data from the file.
There are always three steps that must be taken when a file is used by a
program.

1. Open the file—Opening a file creates a connection between the file and
the program. Opening an output file usually creates the file on the disk and
allows the program to write data to it. Opening an input file allows the
program to read data from the file.

2. Process the file—In this step data is either written to the file (if it is an
output file) or read from the file (if it is an input file).

3. Close the file—When the program is finished using the file, the file must be
closed. Closing a file disconnects the file from the program.
Types of Files

In general, there are two types of files: text and binary. A text file contains data that
has been encoded as text, using a scheme such as ASCII or Unicode. Even if the file
contains numbers, those numbers are stored in the file as a series of characters. As a
result, the file may be opened and viewed in a text editor such as Notepad. A binary
file contains data that has not been converted to text. The data that is stored in a
binary file is intended only for a program to read. As a consequence, you cannot view
the contents of a binary file with a text editor.

Python allows you to work both text files and binary files. You will be able to use a text
editor to examine the files that your programs create.
File Access Methods

Most programming languages provide two different ways to access data stored in a
file: sequential access and direct access. When you work with a sequential access file,
you access data from the beginning of the file to the end of the file. If you want to read
a piece of data that is stored at the very end of the file, you have to read all of the data
that comes before it. In a Sequential File, you cannot jump directly to the desired data.
This is similar to the way cassette tape or old VHS tape players work. If you want to
listen to the last song on a cassette tape, you have to either fast-forward over all of the
songs that come before it or listen to them. There is no way to jump directly to a
specific song.
When you work with a direct access file (which is also known as a random access file),
you can jump directly to any piece of data in the file without reading the data that
comes before it. This is similar to the way a CD player or an MP3 player works. You
can jump directly to any song that you want to listen to.

Sequential access files are easy to work with, and you can use them to gain an
understanding of basic file operations.
Filenames and File Objects

Files are identified by a filename. When you create a document with a word processor and then save the
document in a file, you have to specify a filename. When you use File Management Software to examine
the contents of your disk, you see a list of filenames

Each operating system has its own rules for naming files. Many systems support the use of filename
extensions, which are short sequences of characters that appear at the end of a filename preceded by a
period (which is known as a “dot”). The extension usually indicates the type of data stored in the file. For
example, the .jpg extension usually indicates that the file contains a graphic image that is compressed
according to the JPEG image standard. The .txt extension usually indicates that the file contains text.
The .doc extension (as well as the .docx extension) usually indicates that the file contains a Microsoft Word
document.

In order for a program to work with a file on the computer’s disk, the program must create a file object in
memory. A file object is an object that is associated with a specific file and provides a way for the program
to work with that file. In the program, a variable references the file object. This variable is used to carry out
any operations that are performed on the file
Opening a File

You use the open function in Python to open a file. The open function creates a file object
and associates it with a file on the disk. Here is the general format of how the open
function is used:
file_variable = open(filename, mode)

General Format:
file_variable is the name of the variable that will reference the file object.

filename is a string specifying the name of the file.

mode is a string specifying the mode (reading, writing, etc.) in which the file will be
opened.
Some of the Python file modes

Mode Types and their Description

'r' Open a file for reading only. The file cannot be changed or written to.

'w' Open a file for writing. If the file already exists, erase its contents. If it does not
exist, create it.

'a' Open a file to be written to. All data written to the file will be appended to it
end. If the file does not exist, create it.
After this statement executes, the file named customers.txt will be opened, and the
variable customer_file will reference a file object that we can use to read data from the
file. Suppose we want to
create a file named sales.txt and write data to it.

Here is an example of the opening a file for input

sales_file = open('sales.txt', 'w')


After this statement executes, the file named sales.txt will be created, and the
variable sales_file will
reference a file object that we can use to write data to the file.

Remember, when you use the 'w' mode you are creating the file on the disk. If a file
with the specified
name already exists when the file is opened, the contents of the existing file will be
erased.

You might also like