0% found this document useful (0 votes)
42 views

C++ Unit5

The document discusses windows and file handling in C++. It defines a window as a programming construct that occupies screen space, may be visible or not, knows how to draw itself, and responds to events. It describes two main types of windows - application windows and user interface control windows. The document also covers opening, reading and writing to files, detecting the end of files, and moving between locations in a file using file pointers. It discusses the relationships between application windows, dialog windows, and child windows. Finally, it provides a brief overview of what the Microsoft Foundation Classes library is and what functionality it provides.

Uploaded by

Ch. Rahul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C++ Unit5

The document discusses windows and file handling in C++. It defines a window as a programming construct that occupies screen space, may be visible or not, knows how to draw itself, and responds to events. It describes two main types of windows - application windows and user interface control windows. The document also covers opening, reading and writing to files, detecting the end of files, and moving between locations in a file using file pointers. It discusses the relationships between application windows, dialog windows, and child windows. Finally, it provides a brief overview of what the Microsoft Foundation Classes library is and what functionality it provides.

Uploaded by

Ch. Rahul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Work with File in C++

Introduction
Many real-life problems handle large volumes of data and, in such situations, we need to use some devices
such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of
files. A file is a collection of related data stored in a particular area on the disk. Programs can be designed
to perform the read and write operations on these files.

Console-program-file interaction

Class for File Stream Operations

Opening and Closing Files


A. Opening Files using Constructor:
This involves the following steps:-
1. Create a file stream object to manage the stream using the appropriate class. That is to say, the class
ofstream is used to create the output stream and the class ifstream to create the input stream.
2. Initialize the file object with the desired filename.
For example, the following statement opens a file named “results" for output:
ofstream outfile("resu1ts"); // output only
This creates outfile as an ofstream object that manages the output stream. This object can be any valid C++
name such as o_file, myfile or fout. This statement also opens the file results and attaches it to the output
stream outfile.

Similarly, the following statement declares infile as an ifstream object and attaches it to the file data for
reading (input).
Ifstream infile("data"); // input only
Refer Program 11.1 from the textbook, page no. 329.
B. Opening Files Using open()
As stated earlier, the function open() can be used to open multiple files that use the same stream object.
For example, we may want to process a set of files sequentially. In such cases, we may create a single
stream object and use it to open each file in turn. This is done as follows:
file-stream-class stream-object;
stream-object.open ("filename");

Refer Program 11.2 from the textbook, page no. 331.


Detecting end-of-file
Detection of the end-of-file condition is necessary for preventing any further attempt to read data from the
file. For example, in Program 11.2 by using the statement
while(fin)
An ifstream object, such as fin, returns a value of 0 if any error occurs in the file operation including the
end-of-file condition Thus, the while loop terminates when fin returns a value of zero on reaching the end-
of-file condition.
Another approach to detect the end-of-file condition.
if(fin1.eof() != 0) {exit(1);}
eof() is a member function of ios class. It returns a non-zero value if the end-of-file(EOF) condition is
encountered, and a zero, otherwise. Therefore, the above statement terminates the program on reaching the
end of the file.
File Mode
We have used ifstream and ofstream constructors and the function open() to create new files as well as to
open the existing files. Remember, in both these methods, we used only one argument that was the filename.
However, these functions can take two arguments, the second one for specifying the file mode. The general
form of the function open() with two arguments is:
stream-object.open("filenane", mode);
The second argument mode (called file mode parameter) specifies the purpose for which the file is opened.
In the previous example we open the files without providing the second argument.The prototype of these
class member functions contain default values for the second argument and therefore they use the default
values in the absence of the actual values. The default values are as follows:
ios::in for ifstream functions meaning open for reading only.
ios:out for ofstream functions meaning open for writing only.

File Pointers and Their Manipulations


Each file has two associated pointers known as the file pointers. One of them is called the input pointer (or
get pointer) and the other is called the output pointer (or put pointer). We can use these pointers to move
through the files while reading or writing. The input pointer is used for reading the contents of a given file
location and the output pointer is used for writing to a given file location. Each time an input or output
operation takes place, the appropriate pointer is automatically advanced.
Default Actions
When we open a file in read-only mode, the input pointer is automatically set at the beginning so that we
can read the file from the start. Similarly, when we open a file in write-only mode, the existing contents
are deleted and the output pointer is set at the beginning. This enables us to write to the file from the start.
In case, we want to open an existing file to add more data, the file is opened in 'append' mode. This moves
the output pointer to the end of the file (i.e. the end of the existing contents).

Functions for Manipulation of File Pointers


The file stream classes support the following functions to manage such situations:
seekg() Moves get pointer (input) to a specified location.
seekp() Moves put pointer (output) to a specified location.
tell() Gives the current position of the get pointer.
tellp() Gives the current position of the put pointer
For example, the statement infile.seekg(10); moves the file pointer to the byte number 10. Remember, the
bytes in a file are numbered beginning from zero Therefore, the pointer will be pointing to the 11th byte in
the file.
ofstream fileout;
fileout.open(“hello”, ios::app);
int p =fileout.tellp();
On execution of these statements, the output pointer is moved to the end of the file "hello” and the value of
p will represent the number of bytes in the file.
Specifying the offset

‘Seek' functions seekg() and seekp() can also be used with two arguments as follows:
seekg(offset, refposition)
seekp(offset, refposition)
The parameter offset represents the number of bytes the file pointer is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined
in the ios class:
ios::beg Start of the file
ios::cur current position of the pointer
ios::end End of the file
The seekg() function moves the associated file’s ‘get’ pointer while the seek() function moves the associated
file's ‘put’ pointer.

What Is a Window?
Windows are central to Windows. They are so important that they named the operating system after them.
But what is a window? When you think of a window, you probably think of something like this:
This type of window is called an application window or main window. It typically has a frame with a title
bar, Minimize and Maximize buttons, and other standard UI elements. The frame is called the non-client
area of the window, so called because the operating system manages that portion of the window. The area
within the frame is the client area. This is the part of the window that your program manages.
Here is another type of window:

If you are new to Windows programming, it may surprise you that UI(User Interface) controls, such as
buttons and edit boxes, are themselves windows. The major difference between a UI control and an
application window is that a control does not exist by itself. Instead, the control is positioned relative to the
application window. When you drag the application window, the control moves with it, as you would expect.
Also, the control and the application window can communicate with each other. (For example, the
application window receives click notifications from a button.)
Therefore, when you think window, do not simply think application window. Instead, think of a window as
a programming construct that:
• Occupies a certain portion of the screen.
• May or may not be visible at a given moment.
• Knows how to draw itself.
• Responds to events from the user or the operating system.
• Parent Windows and Owner Windows
In the case of a UI control, the control window is said to be the child of the application window. The
application window is the parent of the control window. The parent window provides the coordinate system
used for positioning a child window. Having a parent window affects aspects of a window's appearance; for
example, a child window is clipped so that no part of the child window can appear outside the borders of
its parent window.
Another relationship is the relation between an application window and a modal dialog window. When an
application displays a modal dialog, the application window is the owner window, and the dialog is an
owned window. An owned window always appears in front of its owner window. It is hidden when the
owner is minimized, and is destroyed at the same time as the owner.
The following image shows an application that displays a dialog box with two buttons:
The application window owns the dialog window, and the dialog window is the parent of both button
windows. The following diagram shows these relations:

MFC
The Microsoft Foundation Class Library (MFC) is an "application framework" for programming in
Microsoft Windows. MFC provides much of the code, which are required for the following −

• Managing Windows.
• Menus and dialog boxes.
• Performing basic input/output.
• Storing collections of data objects, etc.
You can easily extend or override the basic functionality the MFC framework in you C++ applications by
adding your application-specific code into MFC framework.
MFC Framework

• The MFC framework provides a set of reusable classes designed to simplify Windows
programming.
• MFC provides classes for many basic objects, such as strings, files, and collections that are used in
everyday programming.
• It also provides classes for common Windows APIs and data structures, such as windows, controls,
and device contexts.
• The framework also provides a solid foundation for more advanced features, such as ActiveX and
document view processing.
• In addition, MFC provides an application framework, including the classes that make up the
application architecture hierarchy.
Why MFC?

• The MFC framework is a powerful approach that lets you build upon the work of expert
programmers for Windows. MFC framework has the following advantages.
• It shortens development time.
• It makes code more portable.
• It also provides tremendous support without reducing programming freedom and flexibility.
• It gives easy access to "hard to program" user-interface elements and technologies.
• MFC simplifies database programming through Data Access Objects (DAO) and Open Database
Connectivity (ODBC), and network programming through Windows Sockets.
MFC is loosely organized into several major categories:
The library's classes are presented here in the following categories:
• Root Class: CObject
• MFC Application Architecture Classes
• Application and Thread Support Classes
• Command Routing Classes
• Document Classes
• View Classes (Architecture)
• Frame Window Classes (Architecture)
• Document-Template Classes
• Window, Dialog, and Control Classes
• Frame Window Classes (Windows)
• View Classes (Windows)
• Dialog Box Classes
• Control Classes
• Control Bar Classes
• Drawing and Printing Classes
• Output (Device Context) Classes
• Drawing Tool Classes
• Simple Data Type Classes
• Array, List, and Map Classes
• Template Classes for Arrays, Lists, and Maps
• Ready-to-Use Array Classes
• Ready-to-Use List Classes
• Ready-to-Use Map Classes
• File and Database Classes
• File I/O Classes
• DAO Classes
• ODBC Classes
• OLE DB Classes
• Internet and Networking Classes
• Windows Sockets Classes
• Win32 Internet Classes
• OLE Classes
• OLE Container Classes
• OLE Server Classes
• OLE Drag-and-Drop and Data Transfer Classes
• OLE Common Dialog Classes
• OLE Automation Classes
• OLE Control Classes
• Active Document Classes
• OLE-Related Classes
• Debugging and Exception Classes
• Debugging Support Classes
• Exception Classes

MFC - Document View


The Document/View architecture is the foundation used to create applications based on the Microsoft
Foundation Classes library. It allows you to use different parts that compose a computer program including
what the user sees as part of your application and the document a user would work on. This is done through
a combination of separate classes that work as an ensemble.
The parts that compose the Document/View architecture are a frame, one or more documents, and the view.
Put together, these entities make up a usable application.
View
A view is the platform the user is working on to do his or her job. To let the user do anything on an
application, you must provide a view, which is an object based on the CView class. You can either directly
use one of the classes derived from CView or you can derive your own custom class from CView or one of
its child classes.
Document
A document is similar to a bucket. For a computer application, a document holds the user's data. To create
the document part of this architecture, you must derive an object from the CDocument class.
Frame
As the name suggests, a frame is a combination of the building blocks, the structure, and the borders of an
item. A frame gives "physical" presence to a window. It also defines the location of an object with regards
to the Windows desktop.
Refer: https://www.youtube.com/watch?v=8_hP716-eco
Refer: https://www.tutorialspoint.com/mfc/mfc_document_view.htm

You might also like