I/O in C - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

I/O in C

Description:

output streams: from the program. note I use plural ... Snippet. char tData[100]; // This is a method in C istream classes for // inputting text ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 22
Provided by: junaed5
Category:
Tags: snippet

less

Transcript and Presenter's Notes

Title: I/O in C


1
I/O in C
  • October 7, 2008.
  • Junaed Sattar

2
Stream I/O
  • a stream is a flow of bytes/characters/ints or
    any type of data
  • input streams to the program
  • output streams from the program
  • note I use plural
  • one program can have multiple I/O streams
    associated
  • and vice-versa

3
Input/Output
  • Console based, no GUI
  • standard streams
  • cin standard input
  • cout standard output
  • cerr standard error

4
Extraction/Insertion
  • cout ltlt Hello world!
  • cout ltlt The value of i is ltlt i ltlt endl
  • //endl puts a new line
  • cout ltlt Please enter your name
  • string name
  • cin gtgt name

5
What are cin and cout?
  • Stream classes
  • Classes have methods, as we know
  • so does cin and cout
  • some common to all I/O stream classes in C
  • File I/O, binary/text mode I/O, console I/O

6
One example
  • cin inputs ints, chars, null-terminated strings,
    string objects
  • but terminates when encounters space (ascii
    character 32)?
  • workaround?
  • use the get method

7
Snippet
  • char tData100
  • // This is a method in C istream classes
    for
  • // inputting text
  • //including spaces
  • cin.get( tData, 99 )
  • // or
  • cin.get(tData,99,'\n')
  • Inputting i am oh so cool
  • cin.get gets the entire line
  • just cin will get I
  • space termination

8
Or,
  • Use the getline function
  • getline( cin, name )

9
File I/O
  • Reading from or writing to files on disk
  • ifstream and ofstream classes
  • dedicated for input and output respectively
  • or, use fstream

10
Example Files Program(filesdemo)
  • ofstream myofile
  • myofile.open( sample.txt )
  • myofile ltlt This is a sample line I'm writing\n
  • myofile.close()
  • ...
  • ifstream myifile
  • myifile.open( sample.txt )
  • string oneLine
  • getline( myifile, oneLine )
  • cout ltlt oneLine
  • myifile.close()

11
Read/Write to files (files1/2)?
  • Similar to how we use cin and cout
  • remember, these are I/O streams too
  • myfile is a file stream object, then
  • to write an int
  • int i 10myfile ltlt i
  • to read an int
  • int imyfile gtgt i

12
Binary files
  • As opposed to text files, they are unformatted as
    ascii.
  • text files stores everything as ascii text
    strings
  • even numbers
  • binary files do not
  • Example consider outout of the program in the
    previous slide

13
Difference?
  • Example program
  • Accepts student ID (I input 1010)
  • Accepts name (I input Junaed)
  • Accepts CGPA (I input 4.5)
  • Save into two files, as text and binary

14
Storage
TEXT FILE
BINARY FILE
15
Binary files
  • Files by default are text
  • Different methods to write and read
  • requires casting (we'll see casting soon)?
  • different data format
  • If time permits, we'll revisit

16
Failures?
  • If open fails?
  • Check before use
  • if( !myifile ) cerr ltlt Cannot open file!
    exit(1)
  • End of file?
  • while( myifile.fail() ) //do your operations
    here

17
Random vs Sequential
  • Random access files
  • nonsequential,
  • as a result faster access times,
  • content must be suitable for random access
  • for example. not on network streams!
  • or console input

18
File heads
  • Access positions
  • one each for read and write
  • hence two methods
  • seekg (as in get) for reading
  • seekp (as in put) for writing
  • ifstreams have seekg
  • ofstreams have seekp

19
seeking
  • seekg( position, mode) //(same for seekp)?
  • position is a long integer signed offset
  • mode can be
  • iosbeg from the beginning
  • iosend from the end
  • ioscur from current position

20
telling
  • tellg and tellp
  • returns as long integer, the position of the get
    and put positions, respectively

21
example seeks
  • file.seekg( 20L, iosbeg )
  • file.seekp( -100L, ioscur )
  • long pPosition file.tellp()
  • long gPosition file.tellg()
Write a Comment
User Comments (0)
About PowerShow.com