lect08_File_IO

lect08_File_IO - File I/O File I/O CSIS1117 Computer...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
File I/O SIS1117 Computer Programming CS S Co pute og a g 1 c1117 lecture 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ontent Content ile Output Streams File Output Streams File Input Streams Character I/O String Streams c1117 lecture 8 2
Background image of page 2
treams Streams ++ a tream a quence of data In C++ , a stream is a sequence of data . An input stream is a source from which data are ad ne by one read one by one. An output stream is a destination to which data are written one by one. So far, we have learnt the standard input and output streams , the cin and cout . cin : take input from keyboard, e.g. cin >> num cout : send output to the screen, e.g. cout << num c1117 lecture 8 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ile I/O File I/O sing andard put/output streams common Using standard input/output streams is common for small programs that work interactively with the sers users . The no. of inputs is small and just needs to show the utput on the screen. output on the screen. However, it is not appropriate for program that kes a rge number f puts r the utputs re takes a large number of inputs or the outputs are required to be stored permanently in files . g The editor & word processing software manipulate E.g. The editor & word processing software manipulate files directly. c1117 lecture 8 4
Background image of page 4
ile streams File streams o ad/write ata from/to a le we can use the To read/write data from/to a file , we can use the file input/output streams . We can declare/create a file stream variable that is logically connected with the file stored in some directory (e.g. current directory). To create a file output stream Declare a variable with the type ofstream Connect the variable to a file Output your data to the stream (same as using cout) Disconnect the variable from the file in the end. ofstream is defined in the library <fstream> c1117 lecture 8 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ile output stream File output stream Write a program to read in an int n , then print a flag #include <iostream> include <fstream> with size nxn to a file. Include the library #include <fstream> using namespace std; int main(){ Declare the file output stream variable ofstream target; int n; target.open("myresult.txt"); i n> >n ; Connect the riable to a file cin >> n; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j) variable to a file target << "*"; target << endl; Write data to the file as cout c1117 lecture 8 6 } target.close(); } Disconnect the variable
Background image of page 6
ile streams File streams myresult.txt" oes not exist in the current If myresult.txt does not exist in the current directory, a new file will be created. Otherwise, the ntents of the existing file will be erased contents of the existing file will be erased.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/04/2011.

Page1 / 26

lect08_File_IO - File I/O File I/O CSIS1117 Computer...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online