- Prof. amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files Prof. - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files Prof. amr Goneid, AUC 1 Streams & Files Prof. amr Goneid, AUC 2 Streams & Files What are Streams and Files? Default I/O Streams
1
2
3
What are Streams and Files? Default I/O Streams I/O Manipulators External Files Structure of Text Files Declaring Streams Opening & Closing One-Character I/O String & Data I/O Passing Files as Parameters
4
5
6
7
8
9
10
11
Member functions for cin and cout can be used to
12
char c; cin.get(c); // Extract character to c while ( ! cin.eof() ) // Test for EOF (CTRL-Z) { c = toupper (c); // Convert to uppercase cout.put(c) // Insert contents of c to screen cin.get(c); // Get next character }
13
// File: CountChars.cpp // Counts the number of characters and lines in // a file #include <iostream> #include <string> using namespace std; #define ENDFILE "CTRL-Z"
14
int main() { const char NWLN = '\n'; // newline character char next; int charCount; int totalChars; int lineCount; lineCount = 0; totalChars = 0;
15
cout << "Enter a line or press " << ENDFILE << ": "; while (cin.get(next)) { charCount = 0; while (next != NWLN && !cin.eof()) { cout.put(next); charCount++; totalChars++; cin.get(next); } // end inner while
16
cout.put(NWLN); lineCount++; cout << "Number of characters in line " << lineCount << " is " << charCount << endl; cout << "Enter a line or press " << ENDFILE << ": "; } // end outer while cout << endl << endl << "Number of lines processed is " << lineCount << endl;
17
cout << "Total number of characters is " << totalChars << endl; return 0; }
18
19
element
20
Elements are single character or strings with EOLN Access is Sequential Opened in a Single Mode (input or output but not
21
Use #include <fstream> for external text file
To declare an input file stream:
To declare an output file stream:
C++ uses internal name for I/O operations
22
To access a disk file, we have to associate it with
We do this through the .open function. A file has a DOS name that can be saved in a
or read the name from the keyboard, e.g.
23
24
25
26
27
You can test if a file opened correctly. The .fail()
Example: source.open(inFile.c_str()); if (source.fail ()) { cerr << "*** ERROR: Cannot open " << inFile << endl; return 1 // failure return } // end if
28
/* Declare two streams, one for input, the other for output. Attach them to two physical files, e.g. “data1.txt” and “data2.txt” */
#include <iostream> #include <fstream> string infile , outfile; ifstream source;
cout << “Enter Input File Name: “; cin >> infile; cout << “Enter Output File Name: “; cin >> outfile; source.open ( infile.c_str() ); target.open (outfile.c_str() );
29
30
31
32
33
34
char ch; int number ifstream source; source.open(“mydata”); source.get(ch); // get one character if (isdigit(ch)) { source.unget(); source >> number; } source.close();
35
36
37
/* Fuction to copy a file with a name stored in the string infile to another file with a name stored in string outfile (character by character) */ void copychar(string infile, string outfile) { char c; ifstream source;
source.open(infile.c_str()); target.open(outfile.c_str()); source.get(c); while (! source.eof()) { target.put(c); source.get(c); } source.close(); target.close(); }
38
/* A main function to drive copychar */ #include <iostream> #include <fstream> void copychar (string , string ); int main() { string infile , outfile; cout << “Enter Input File Name: “; cin >> infile; cout << “Enter Output File Name: “; cin >> outfile; copychar (infile,outfile); }