Interactive IO - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Interactive IO

Description:

C can write and read from files just as to keyboard and ... TXT - Text files(ASCII form, printable) .DAT - data files. file is composed of tracks, sectors ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 19
Provided by: BlairT6
Category:

less

Transcript and Presenter's Notes

Title: Interactive IO


1
Interactive I/O
  • Input from keyboard
  • Must prompt user
  • User friendly
  • echo checking

2
Noninteractive processing
  • Batch processing
  • Store values in a file
  • File input and Output
  • File - section of secondary storage with a name
  • C can write and read from files just as to
    keyboard and screen
  • standard input file - keyboard
  • standard output file - terminal or printer

3
Advantages to noninteractive
  • can edit data, prepare ahead of time
  • Can rerun file without reentering data
  • Can examine output at leisure
  • Can display output to screen or print
  • Can use out as input into another program
  • means of communicating between programs

4
files
  • file - named secondary memory area that holds a
    collection of information
  • usually stored on an auxiliary storage device
  • examples
  • external file - a file that is used to
    communicate with people or programs. It is stored
    externally to the program.
  • internal file - a file that is created but not
    saved
  • (scratch file)

5
text files
  • composed of characters only
  • Differs from .doc file
  • Size of file
  • Can be created in notepad, Visual c.net

6
File organization
  • naming rules(dos) - 1 to 8 character name,
    optionally followed by a period and up to a 3
    character extension
  • extension tells the type of file
  •  .cpp c program file
  • .doc Word file
  • .exe executable file
  • .OBJ - Compiled programs - object files
  • .TXT - Text files(ASCII form, printable)
  • .DAT - data files
  • file is composed of tracks, sectors
  • FAT table

7
commands
8
files
  • program files - contains program instructions
  • data files - contains data, collection of records
  • record - one entity
  • i.e. customer file - each record holds data for
    each customer
  •  fields - data element
  •  customer file has name field, account field,...
  • record key or key field

9
File Organization
  • Sequential - records stored one after another
  • Random - use key to directly access record
  • Indexed - combines sequential and random
  •  ISAM - Indexed sequential
  •  each record has a unique key
  •  input records arranged in key sequence
  •  one area for index - gives location of record

10
Writing Data to a File
  • 1. Open the file as an output file.
  • 2. Write the elements.
  • 3. Close the file.
  • Data will be output from the program and written
    on the disk. This is used to create a data file. 

11
Opening for output causes
  • The directory is searched for "filename". If it
    doesn't exist, it is created. If it does exist,
    it will be lost. The file is set to write mode.
  • A buffer is created in memory. WRITE causes data
    to go into the buffer. When buffer is filled,
    then gets written to disk.
  • A file pointer is set to the beginning of file.
    This is updated with each write.

12
Reading the Data
  • Open the file for input.
  • Read the data (and print).
  • Close the file. 
  • Data will be input into the program from the
    disk. Reads data previously stored on the disk.

13
Opening for input causes
  • The directory is searched for "filename". The
    file is set to read mode.
  • A buffer is created in memory.
  • A file pointer is set to the beginning of file.

14
Using Files in c
  • 1. include ltfstream.hgt
  • 2.declare file streamsifstream inFile //input
    only (reading)ofstream outFile //output only
    (writing)
  • 3. open each file for reading or writing
    filevar.open(filename)
  • 4.specify names of file streams in input and
    output statement

15
Declaring Files
  • ifstream input file stream
  • ofstream output file streamgtgt, get, ignore
    also valid for ifstreamltlt endl,setw,
    setprecision also valid for ostream
  • declareifstream inMPGofstream outMPG

16
Open Files
  • inMPG.open("inmpg.dat")outMPG.open("outmpg.dat")
    1. associates a stream variable with file2.
    output file
  • check if it exists, overwrite, if not create
    sets marker to beginning of file
  • good idea to open all files at the beginning

17
input and output-
  • use var name in input and output statements in
    place of cin and coutinMPG gtgt amt1 gtgt amt2 gtgt
    amt3 gtgt amt4 gtgt startMiles
  • gtgt endMiles
  • outMPG ltlt "the mileage per gallon is " ltlt mpg ltlt
    endl

18
Input Failure
  • invalid data -gt fail state
  • after fail state, any further i/o operations have
    no effect
  • opening file that doesn't exist -gt fail state
  • Important to test for this!!!!!!inMPG.open("inmpg
    .dat")
  • if(!inMPG)
  • cout ltlt Cant open inmpg.dat
  • return 1
Write a Comment
User Comments (0)
About PowerShow.com