C Stream IO Including Files - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

C Stream IO Including Files

Description:

must 'open' and 'close' stream for actual file before & after. ... Close the stream. Repeated Input. Reading columnar input with a loop, ... Close the stream. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 12
Provided by: cscb6
Category:

less

Transcript and Presenter's Notes

Title: C Stream IO Including Files


1
C Stream I/OIncluding Files
  • R. Johnson

2
Stream I/O
  • cin and cout are streams
  • Stream I/O is made up of series of characters
  • Work of I/O is performed by ltlt and gtgt operators
    cin, cout are source, destination
  • gtgt and ltlt translate data types to/from chars
  • Cin, cout act as middle man between our program
    and the hardware operating system!
  • Objects predefined (declared) globally in
    ltiostreamgt

3
File Stream I/O
  • Streams also used for file I/O (to disk).
  • include ltfstreamgt
  • Must explicitly declare ourselves.
  • File stream is NOT the file on disk!!!!!
  • Formatting, actual I/O exactly like cin/cout.
  • BUT must open and close stream for actual
    file before after.
  • Text files always read/written from beginning
    (sequential).
  • Current position advances thru file with each I/O
    operation.

4
File Stream Output
  • include ltfstreamgt
  • Declare ofstream prt
  • Open creates/overwrites existing
    prt.open(data.txt)
    prt.open(filename.c_str())
  • Append? prt.open(filename.c_str(),iosapp)
  • Formatting prt.setf( prt.precison(
  • Output prt ltlt var1 ltlt var2ltlt endl
  • Close prt.close()

5
File Stream InputMUST know layout of input file!
  • Declare ifstream read
  • Open read.open(filename.c_str())
  • Exist? if (read.fail())
    cout ltlt File not found!
    exit(1)
  • Input read gtgt var1 gtgt var2 treats
    spaces, newline just like cingtgt
  • Close read.close()

6
Repeated OutputProduce columnar output with
loops (while,for)
  • Declare open stream
  • Headers lines? Formatting?
  • Start of loop getting input or generating data
  • Get input
  • Use the data calculations?
  • Output a line to the file.
  • End of loop
  • Close the stream.

7
Repeated InputReading columnar input with a
loop, works with unknown of lines!
  • Declare open stream
  • Read any non-repeating header lines
  • While not at end-of-file yet while(!read.eof())
  • Get input data current line
  • Use the data calculations, output?
  • End of loop
  • Close the stream.
  • PROBLEM when using gtgt, can stumble on presence
    absence of final newline char!!!!

8
Repeated Input-IMPROVED-
  • Declare open stream
  • Read any non-repeating header lines
  • While an input succeeds while(readgtgtxgtgty)
  • Use the data calculations, output?
  • End of loop
  • Close the stream.
  • FIX when using gtgt, will NOT stumble on
    presence/absence of final newline char
  • WHY? Value of stream read looks like True after
    a successful read, like False on attempt to read
    past end of file!

9
Problem with string input form keyboard and file
  • cin gtgt and read gtgt see blank spaces as
    separating real values.
  • What about entire line of input as one string,
    including spaces?
  • getline(cin, stringVar) for ltstringgt
  • Reads all chars up to \n (newline char)
  • Discards closing \n without putting it in string
    variable!
  • NOTE cin.getline() is NOT the same!!

10
Problem with getline()
  • Using getline() after cingtgt can give empty
    string.
  • Why? cingtgt sees \n as separator, but leaves it
    on the stream!
  • A following getline() ends before ever getting ot
    next real line of input.
  • FIX cingtgt x cin.ignore(80,\n)
    getline(cin, header1)cin gtgt y
    //Note NOT a problem after!

11
Summary getline() and .ignore()
  • cingtgtname user types Jim Dye
  • gtgt operator only puts Jim into string name
    because it sees any spaces as separating the
    inputs.
  • getline(cin, name) reads in all characters
    until \n is encountered.
  • Usually need to use cin.ignore(80,\n) between
    using cin gtgt and getline() in that order.
  • Use cin.getline(name) ONLY if name is declared
    as a c-string instead of standard string!
Write a Comment
User Comments (0)
About PowerShow.com