You are on page 1of 14

Basic Programming for Electrical Engineers

C File Processing
Introduction

• Storage of data in variables and arrays is temporary. Files


are used for permanent retention of data. Computers
store files on secondary storage devices such as disk
storage devices.
Introduction

• A record (i.e., a struct in C) is composed of several fields.


In a payroll system, for example, a record for a particular
employee might consist of the following fields:
– Social Security Number (alphanumeric field)
– Name (alphabetic field)
– Address (alphanumeric field)
– Hourly salary rate (numeric field)
• A record is a group of related fields.
• A file is a group of related records.
Data Hierarchy
Introduction

• To facilitate the retrieval of specific records from a file, at


least one field in each record is chosen as record key. A
record key identifies a record as belonging to a particular
person or entity.
• There are many ways of organizing records in a file. The
most popular type of organization is called a sequential
file, in which records are typically stored in order by the
record key field.
Introduction

• Most business store data in many different files:


– payroll files
– accounts receivable files
– accounts payable files
– inventory files
• A group of related files is sometimes called a database.
• A collection of programs designed to create and manage
databases is called a database management
system(DBMS).
Files and Streams

• C views each file simple as a sequential stream of bytes.


Each file ends either with an end-of-file marker or at a
specific byte number recorded in a system-maintained,
administrative data structure.
• Opening a file returns a pointer to a FILE structure
(defined in <stdio.h>
Using Filenames

• Every disk file has a name and you must use filenames
when dealing with disk files. Filenames are stored as
strings. The rules that establish what is acceptable for
filenames differ from one operating system to another.
• A filename in a C program can contain path information.
the path specifies the drive and/or directory where the file
is located.
Opening a File

• The process of creating a stream link to a disk file is


called opening the file. When you open a file, it becomes
available for reading, writing, or both. When you're done
using the file, you must close it.
• To open a file, you use the fopen() library function
<stdio.h>
Writing and Reading File Data
• A program that uses a disk file can write a data to a file,
read data from a file, or a combination of the two. Three
ways of writing data to a file:
– Using formatted output to save formatted data to a file. Its
primary use is to create files with text and numeric data to be
read by other programs such as spreadsheets or databases.
– Using a character output to save single characters or lines of
characters to a files. Its primary use is to save text (not
numeric) data in a form that can be read by C as well as other
word processors.
– Using direct output to save the contents of a section of memory
directly to a disk file. This is for binary files only. This is the best
way to save data for later use by a C program.
Formatted File Input and Output

• Formatted File Output

• Formatted File Input

You might also like