{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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 CSIS1117 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 Document Right Arrow Icon
Content File Output Streams File Output Streams File Input Streams Ch t I/O Character I/O String Streams c1117 lecture 8 2
Background image of page 2
Streams Streams In C++ a stream is a sequence of data In , a is a . An input stream is a source from which data are read one by one one by one. An output stream is a destination to which data itt b 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 Document Right Arrow Icon
File I/O File I/O Using standard input/output streams is common Using is common for small programs that work interactively with the users . The no. of inputs is small and just needs to show the output on the screen. However, it is not appropriate for program that takes a large number of inputs or the outputs are takes a or the outputs are required to be stored permanently in files . E 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
File streams File streams To read/write data from/to a file , we can use the To data from/to a , 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) h f h f l h d 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 Document Right Arrow Icon
File output stream Write a program to read in an intn , then print a flag with size to a file #include <iostream> #include <fstream> with size nxn to a file. Include the library using namespace std; int main(){ Declare the file output stream variable ofstream target; int n; target.open("myresult.txt"); cin >> n; Connect the variable to a file for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j) W it d t t target << "*"; target << endl; } Write data to the file as cout c1117 lecture 8 6 target.close(); } Disconnect the variable
Background image of page 6
File streams If "myresult.txt" does not exist in the current If myresult.txt does not exist in the current directory, a new file will be created. Otherwise, the contents of the existing file will be erased contents of the existing file will be erased. See output-file.cc as an example. T t fil i To create a file input stream Declare a variable with the type ifstream Connect the variable to a file Read data from the stream as using cin Disconnect the variable from the file in the end.
Background image of page 7

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

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

{[ snackBarMessage ]}