# Chapter 18: The C++ I/O System

### Example 18.1, Page Number: 421

In [1]:
class three_d:
def __init__(self,a,b,c):   #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
def __repr__(self):
return str(self.x)+", "+str(self.y)+", "+str(self.z)+"\n"

#Variable declaration
a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)

#Result
print a,b,c

1, 2, 3
3, 4, 5
5, 6, 7



### Example 18.2, Page Number: 423

In [2]:
class thrnee_d:
def __init__(self,a,b,c):   #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
__repr__=repr

#Friend function
def repr():
return str(self.x)+", "+str(self.y)+", "+str(self.z)+"\n"

#Variable declaration
a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)

print a,b,c

1, 2, 3
3, 4, 5
5, 6, 7



### Example 18.3, Page Number: 424

In [3]:
class three_d:
def __init__(self,a,b,c):   #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
def __repr__(self):
return str(self.x)+", "+str(self.y)+", "+str(self.z)

#Variable declaration
a=three_d(1,2,3)

print a

#User input
print "Enter X,Y,Z values:"
a=three_d(4,5,6)
print a

1, 2, 3
Enter X,Y,Z values:
4, 5, 6


### Example 18.4, Page Number: 428

In [4]:
print '{0:+d}'.format(123),   #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23)  #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)


+123 +1.232300e+02


### Example 18.5, Page Number: 430

In [1]:
import string

print '{0:+d}'.format(123),        #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23)       #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)

print '{:10.2f}'.format(123.23),   #2 digits left of decimal
if(123.23>0):
i='{0:.2e}'.format(123.23)     #for ios::scientific
i='+'+i
print i
else :
print '{0:.2e}'.format(123.23)

print '{:#>10}'.format(str(123)),  #for ios::fill
if(123.23>0):
i='{0:.2e}'.format(123.23)  #for ios::scientific
i='+'+i
print i
else :
print '{0:.2e}'.format(123.23)

+123 +1.232300e+02
123.23 +1.23e+02
#######123 +1.23e+02


### Example 18.6, Page Number: 432

In [5]:
print '{0:.0e}'.format(1000.243)        #for setprecision
print '{:>20}'.format("Hello There")    #to set width and right align

1e+03
Hello There


### Example 18.7, Page Number: 433

In [6]:
print '{0:+d}'.format(123),   #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23)  #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)


+123 +1.232300e+02


### Example 18.8, Page Number: 433

In [7]:
#User input
s="   Hello"

#Result
print s.lstrip()    #lstrip removes leading spaces

Hello


### Example 18.9, Page Number: 434

In [2]:
def setup(s):
return '{:\$<10}'.format(str(s))

#Result
print 10,setup(10)

10 10


### Example 18.10, Page Number: 435

In [ ]:
def prompt():
print "Enter number using hex format:"
hex=0x46
return hex

#Result
i=prompt()
print i


### Example 18.11, Page Number: 438

In [8]:

out=open("test",'w')

if(not(out)):
print "Cannot open file."
else:
#Write to file
out.write("10 123.23\n")
out.write("This is a short text file.")
#Close the file
out.close()


### Example 18.12, Page Number: 438

In [9]:

In=open("test",'r')

if(not(In)):
print "Cannot open file."
else:
print i,f,ch
print str
#Close the file
out.close()

10 123.23

This is a short text file.


### Example 18.13, Page Number: 439

In [10]:
import sys

if not(len(sys.argv)==2):
print "Usage: PR <filename>\n"
else:
#Open a file
In=open(sys.argv[1],'r')

#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
print ch
In.close()


Usage: PR <filename>



### Example 18.14, Page Number: 440

In [11]:
import sys

p="hello there"

out=open("test",'w')

#In case file cannot open
if(not(out)):
print "Cannot open file."
else:
#Write to file
for i in range(len(p)):
out.write(p[i])


### Example 18.15, Page Number: 441

In [12]:
import sys

n=[1,2,3,4,5]

#Open a file 'test'
out=open("test",'w')

#In case file cannot open
if(not(out)):
print "Cannot open file."
else:
#Write to file
for i in range(5):
out.write(chr(n[i]))
out.close()

for i in range(5):         #clear array
n[i]=0

#Open the file
In=open("test",'r')

#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
for i in range(5):

#Result, shows value from file
for i in range(5):
print n[i],

In.close()

1 2 3 4 5


### Example 18.16, Page Number: 442

In [13]:
import sys

if not(len(sys.argv)==2):
print "Usage: PR <filename>\n"
else:
#Open a file
In=open(sys.argv[1],'r')

#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
while True:
if not ch:
break
print ch,
#Close file
In.close()


Usage: PR <filename>



### Example 18.17, Page Number: 443

In [14]:

out=open("test1",'w')
out.write("Hello")
out.close()

out=open("test2",'w')
out.write("There")
out.close()

f1=open("test1",'r')

if(not(In)):
print "Cannot open file."

f2=open("test2",'r')

if(not(In)):
print "Cannot open file."

print "Comparing files..."

print buf1,buf2

if len(buf1)==len(buf2):
print "Files are of different sizes."
f1.close()
f2.close()
else:

flag=1
for i in range(len(buf1)):
if not(buf1[i]==buf2[i]):
print "Files differ."
f1.close()
f2.close()
flag=0
break
if flag==1:
print "Files are the same."


Comparing files...
Hello There
Files are of different sizes.


### Example 18.18, Page Number: 445

In [15]:
print "Enter your name:"

str="hello world"

print str

Enter your name:
hello world


### Example 18.19, Page Number: 447

In [2]:
 out=open("test",'r+b')
out.write("Hello")

if(not(out)):
print "Cannot open file."
else:
out.seek(2,0)
out.write('X')
out.close()



### Example 18.20, Page Number: 447

In [3]:

out=open("test",'r+b')
out.write("Hello")
out.close()

In=open("test",'r')

if(not(In)):
print "Cannot open file."
else:
In.seek(2,0)
print ch


llo


### Example 18.21, Page Number: 450

In [18]:
class three_d:
def __init__(self,a,b,c):
self.__x=a
self.__y=b
self.__z=c
def __repr__(self):
c=("%d"%self.__x)+", "+("%d"%self.__y)+", "+("%d"%self.__z)+"\n"
return c

a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)

out=open("threed",'w')

if(not(out)):
print "Cannot open file."
else:
out.write(a.__repr__())
out.write(b.__repr__())
out.write(c.__repr__())