After Azrael Mac OS
Overview of File Output
Basics:OS X Mountain Lion; Mac OS X v10.7 Lion; Mac OS X v10.6 Snow Leopard; Mac OS X v10.5 Leopard; Mac OS X v10.4 Tiger; Mac OS X v10.3 and earlier; Mac OS X Technologies; Classic Mac OS (OS9, OS8 & System 7) Photos for Mac; QuickTime; Safari; Front Row. AirPods Support. Forgot Apple ID or password. Billing and subscriptions. Trade in with Apple. Turn an eligible device into credit towards a new one, or recycle it for free. Apple Trade In is good for you and the planet. YES Multi-user quantity: 100 EZ Cat Disk Cataloger v2.3: Name: Azrael PC Code: 0DBDBD7B EZE Clock v1.0: Email: RepublicRox Code: eZip Wizard v2.10: Name: Versus s/n: 63019506 EZ Mail 16-Bit: Name: ThE STaRDoGG CHaMPioN PC97 s/n: 70908419 EZ Mail Win32/NT: Name: ThE STaRDoGG CHaMPioN PC s/n: 70908403 EZ-Pix v1.0b: Name: REKiEM.
- In C, we used global functions like fopen, fgets, fputs, fprintf, and fclose to open, read, write, and close files.
- In C++, we will be using stream objects (instantiated from stream classes) to perform all of the file input and output.
- This will give us a consistent interface for all I/O.
- If you know how to use cin and cout, then you have 90% of the necessary knowledge. (cin and cout are stream objects themselves.)
- As a class, the stream objects have many members that you can access. The most important ones are the overloaded operators: operator<< and operator>>
- ifstream is an input file stream (for reading input)
- ofstream is an output file stream (for writing output)
- Unlike the default input/output streams cin, cout and cerr, there are no default file streams. You must explicitly open them yourself.
Code | Output in foo.txt (showing the new lines) |
---|
For automatic objects (stack-based), we don't need to call close since it will be done in the destructor.
We can open the file in the constructor:
Streaming a user-defined type:Code | Output (in file1.txt) |
---|
Student class |
---|
StopWatch class |
---|
Rebinding files:
Code | Output (in files) |
---|
- What is sizeof(fnames) ?
- What is sizeof(fnames[1]) ?
- What is sizeof(*fnames[1]) ?
vector<const char *> | vector<string> |
---|
- What is sizeof(fnames)? In other words, what is sizeof(std::vector<const char *>) ?
- Newer compilers (libraries) overload ofstream.open() to take std::string
This code will work with any container:
An even more C++-like implementation might look like this:
Or with C++11 uniform initializer syntax:Overview of File Input
File input is very similar to the use of cin. Given the text in the file foo.txt from above and showing the spaces and newlines: We can read the words back into the program one at at time:Code | Output |
---|
Using FILE pointers | Using C++ streams |
---|
Reading in an entire line at a time:
Using FILE pointers w/C-style strings | Using streams w/std::string |
---|
Using FILE pointers w/C-style strings | Using streams w/std::string |
---|
File Modes
In C, we opened files with the fopen function. It had a signature like this: The mode parameter specified different attributes of the file: C++ streams use a slightly different approach to modes using flags. These flags are very similar to the ones used by the cin and cout objects and modify their behavior. Intro to I/O.Mode | Meaning |
---|---|
ios_base::in | Open file for input (default for ifstream) |
ios_base::out | Open file for output (default for ofstream) |
ios_base::app | Seek to the end before each write (append) |
ios_base::trunc | Truncate file (delete contents) after opening (default for ofstream) |
ios_base::binary | Open file in binary mode |
Sample usage:
More information on C++ streams
In-Depth Example
The assignment is to create a program similar to the Unix (Cygwin) program wc. (Reference) Running the command: produces this output:High-Level Plan | Detailed Plan |
---|---|
|
|
Variation #1 on the main function (std::for_each
After Azrael Mac Os 11
):Variation #2 on the main
After Azrael Mac Os Download
function (no intermediate vector): A closer look atAfter Azrael Mac Os Catalina
for_each: These are the files that need to be included: Running the program: Output:Cygwin/macOS/Linux (wc) | Our program (mywc) |
---|
The complete program.
Exercise for students: Add the totals to the output.
Also, there is a caveat with this program: It reads files in one line-at-a-time and assumes that the files are text files. It also assumes that newlines are a single-character (LF) like Linux/Mac OS X, not two characters (CR/LF) like Windows. You would need to modify the file reading logic to handle both systems. Or, read in characters instead of lines and then account for whitespace to delimit words and newlines to delimit lines. This example chose to keep it simple to illustrate the file I/O.