Download presentation
Presentation is loading. Please wait.
1
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo
2
Lecture Review Character I/O Constant Declaration: const char star = '*'; Variable Declaration: char letter; Variable Assignment : letter = 'A'; letter = 65; Input/Output: cin >> t >> x >> y >> z; cout << t << x << y << z << endl;
3
Lecture Review Predefined Function in Classifying functions isupper(c) islower(c) isdigit(c) isalpha(c) isalnum(c) isspace(c) Conversion functions tolower(c) toupper(c)
4
Lecture Review Read/Write One Character get(char& character) Reads a single character from input stream Including whitespace Stored in character Syntax: char character; cin.get(character); put(char character) Writes a single character to the output stream From character Syntax: char character = ‘a’; cout.put(character);
5
Lecture Review File I/O File External collection of data Provided input to a program Stored output from a program Stored on secondary storage permanently Must be Opened before used Closed after used File I/O Operation related to file
6
Lecture Review Stream Sequence of characters – user interactive cin Input stream associated with keyboard cout Output stream associated with display – file operation ifstream Defined input stream associated with file ofstream Defined output stream associated with file
7
Lecture Review E.g. #include … ifstream fin; … fin.open("filename.txt"); // connects stream fsIn to the external // file "filename.txt" … fin.close(); // disconnects the stream and associated file …
8
Lecture Review Read From File Include the #include Declare an input stream object ifstream fin; Open an dedicated file fin.open(“filename.txt”); Read from file file fin>>a; fin.get(b); Close the file fin.close();
9
Lecture Review Write to File Include the #include Declare an input stream object ofstream fout; Open an dedicated file fout.open(“filename.txt”); Read from file file fout<<a; fout.put(b); Close the file fout.close();
10
Lecture Review File I/O – Function Read from file fin.eof() Test for end-of-file condition fin.get(char& character) Obtains the next character from the file stream Put into the variable character Output file fout.put(char character) Inserts character to the output file stream
11
Lecture Review E.g. … ifstream fin; char character; fin.open(“data.txt”);// open the file fin.get(character);// get the first char while(!fin.eof()){// loop to read each character … fin.get(character); … } fin.close(); …
12
Summary By the end of this lab, you should be able to: Read/Write/Manipulate char data type Read/Write/Manipulate file with directive
13
Lab8 Download the template from website
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.